Пример #1
0
 public function initialize()
 {
     Events::fire("framework.database.initialize.before", array($this->type, $this->options));
     if (!$this->type) {
         $configuration = Registry::get("configuration");
         if ($configuration) {
             $configuration = $configuration->initialize();
             $parsed = $configuration->parse("configuration/database");
             if (!empty($parsed->database->default) && !empty($parsed->database->default->type)) {
                 $this->type = $parsed->database->default->type;
                 unset($parsed->database->default->type);
                 $this->options = (array) $parsed->database->default;
             }
         }
     }
     if (!$this->type) {
         throw new Exception\Argument("Invalid type");
     }
     Events::fire("framework.database.initialize.after", array($this->type, $this->options));
     switch ($this->type) {
         case "mysql":
             return new Database\Connector\Mysql($this->options);
             break;
         default:
             throw new Exception\Argument("Invalid type");
             break;
     }
 }
Пример #2
0
 public function initialize()
 {
     Events::fire("framework.session.initialize.before", array($this->type, $this->options));
     if (!$this->type) {
         $configuration = Registry::get("configuration");
         if ($configuration) {
             $configuration = $configuration->initialize();
             $parsed = $configuration->parse("configuration/session");
             if (!empty($parsed->session->default) && !empty($parsed->session->default->type)) {
                 $this->type = $parsed->session->default->type;
                 unset($parsed->session->default->type);
                 $this->options = (array) $parsed->session->default;
             }
         }
     }
     if (!$this->type) {
         throw new Exception\Argument("Invalid type");
     }
     Events::fire("framework.session.initialize.after", array($this->type, $this->options));
     switch ($this->type) {
         case "server":
             return new Session\Driver\Server($this->options);
             break;
         default:
             throw new Exception\Argument("Invalid type");
             break;
     }
 }
Пример #3
0
 public function __construct($options = array())
 {
     parent::__construct($options);
     // connect to database
     $database = Registry::get("database");
     $database->connect();
     // schedule: load user from session
     Events::add("framework.router.beforehooks.before", function ($name, $parameters) {
         $session = Registry::get("session");
         $controller = Registry::get("controller");
         $user = $session->get("user");
         if ($user) {
             $controller->user = \User::first(array("id = ?" => $user));
         }
     });
     // schedule: save user to session
     Events::add("framework.router.afterhooks.after", function ($name, $parameters) {
         $session = Registry::get("session");
         $controller = Registry::get("controller");
         if ($controller->user) {
             $session->set("user", $controller->user->id);
         }
     });
     // schedule: disconnect from database
     Events::add("framework.controller.destruct.after", function ($name) {
         $database = Registry::get("database");
         $database->disconnect();
     });
 }
Пример #4
0
 /**
  * Parses the HTML file for the action
  * @return string
  */
 public function render()
 {
     Events::fire("framework.view.render.before", array($this->file));
     if (!file_exists($this->file)) {
         return "";
     }
     return $this->template->parse(file_get_contents($this->file))->process($this->data);
 }
Пример #5
0
 public function initialize()
 {
     Events::fire("framework.configuration.initialize.before", array($this->type, $this->options));
     if (!$this->type) {
         throw new Exception\Argument("Invalid type");
     }
     Events::fire("framework.configuration.initialize.after", array($this->type, $this->options));
     switch ($this->type) {
         case "ini":
             return new Configuration\Driver\Ini($this->options);
             break;
         default:
             throw new Exception\Argument("Invalid type");
             break;
     }
 }
Пример #6
0
 function request($method, $url, $parameters = array())
 {
     Events::fire("framework.request.request.before", array($method, $url, $parameters));
     $request = $this->_request = curl_init();
     if (is_array($parameters)) {
         $parameters = http_build_query($parameters, "", "&");
     }
     $this->_setRequestMethod($method)->_setRequestOptions($url, $parameters)->_setRequestHeaders();
     $response = curl_exec($request);
     if ($response) {
         $response = new Request\Response(array("response" => $response));
     } else {
         throw new Exception\Response(ucfirst(curl_error($request)));
     }
     Events::fire("framework.request.request.after", array($method, $url, $parameters, $response));
     curl_close($request);
     return $response;
 }
Пример #7
0
 public function __destruct()
 {
     Events::fire("framework.controller.destruct.before", array($this->name));
     $this->render();
     Events::fire("framework.controller.destruct.after", array($this->name));
 }
Пример #8
0
 public function __construct($options = array())
 {
     parent::__construct($options);
     // connect to database
     $database = Registry::get("database");
     $database->connect();
     $mongoDB = Registry::get("MongoDB");
     if (!$mongoDB) {
         $mongo = new \MongoClient();
         $mongoDB = $mongo->selectDB("stats");
         Registry::set("MongoDB", $mongoDB);
     }
     $session = Registry::get("session");
     $fbapp = $session->get("fbapp");
     if (!$fbapp) {
         $fbapp = \Meta::first(array("property = ?" => "fbapp"))->value;
         $session->set("fbapp", $fbapp);
     }
     $this->getLayoutView()->set("fbapp", $fbapp);
     // schedule: load user from session
     Events::add("framework.router.beforehooks.before", function ($name, $parameters) {
         $session = Registry::get("session");
         $controller = Registry::get("controller");
         $user = $session->get("user");
         if ($user) {
             $controller->user = \User::first(array("id = ?" => $user));
         }
     });
     // schedule: save user to session
     Events::add("framework.router.afterhooks.after", function ($name, $parameters) {
         $session = Registry::get("session");
         $controller = Registry::get("controller");
         if ($controller->user) {
             $session->set("user", $controller->user->id);
         }
     });
     // schedule: disconnect from database
     Events::add("framework.controller.destruct.after", function ($name) {
         $database = Registry::get("database");
         $database->disconnect();
     });
 }
Пример #9
0
 public function __construct($options = array())
 {
     parent::__construct($options);
     Services\Db::connect();
     // schedule: load user from session
     Events::add("framework.router.beforehooks.before", function ($name, $parameters) {
         $session = Registry::get("session");
         $controller = Registry::get("controller");
         $user = $session->get("user");
         if ($user) {
             $controller->user = \User::first(array("id = ?" => $user));
         }
     });
     // schedule: save user to session
     Events::add("framework.router.afterhooks.after", function ($name, $parameters) {
         $session = Registry::get("session");
         $controller = Registry::get("controller");
         if ($controller->user) {
             $session->set("user", $controller->user->id);
         }
         // Set Flash Message to the Action View
         $flashMessage = $session->get('$flashMessage', null);
         if ($flashMessage) {
             $session->erase('$flashMessage');
             $controller->actionView->set('message', $flashMessage);
         }
     });
 }
Пример #10
0
 protected function _pass($controller, $action, $parameters = array())
 {
     $name = ucfirst($controller);
     $this->_controller = $controller;
     $this->_action = $action;
     Events::fire("framework.router.controller.before", array($controller, $parameters));
     try {
         $instance = new $name(array("parameters" => $parameters));
         Registry::set("controller", $instance);
     } catch (\Exception $e) {
         throw new Exception\Controller("Controller {$name} not found");
     }
     Events::fire("framework.router.controller.after", array($controller, $parameters));
     if (!method_exists($instance, $action)) {
         $instance->willRenderLayoutView = false;
         $instance->willRenderActionView = false;
         throw new Exception\Action("Action {$action} not found");
     }
     $inspector = new Inspector($instance);
     $methodMeta = $inspector->getMethodMeta($action);
     if (!empty($methodMeta["@protected"]) || !empty($methodMeta["@private"])) {
         throw new Exception\Action("Action {$action} not found");
     }
     $hooks = function ($meta, $type) use($inspector, $instance) {
         if (isset($meta[$type])) {
             $run = array();
             foreach ($meta[$type] as $method) {
                 $hookMeta = $inspector->getMethodMeta($method);
                 if (in_array($method, $run) && !empty($hookMeta["@once"])) {
                     continue;
                 }
                 $instance->{$method}();
                 $run[] = $method;
             }
         }
     };
     Events::fire("framework.router.beforehooks.before", array($action, $parameters));
     $hooks($methodMeta, "@before");
     Events::fire("framework.router.beforehooks.after", array($action, $parameters));
     Events::fire("framework.router.action.before", array($action, $parameters));
     call_user_func_array(array($instance, $action), is_array($parameters) ? $parameters : array());
     Events::fire("framework.router.action.after", array($action, $parameters));
     Events::fire("framework.router.afterhooks.before", array($action, $parameters));
     $hooks($methodMeta, "@after");
     Events::fire("framework.router.afterhooks.after", array($action, $parameters));
     // unset controller
     Registry::erase("controller");
 }