Exemplo n.º 1
0
 public function initialize()
 {
     Event::fire("framework.cache.initialize.before", array($this->_driver, $this->_options));
     if (!$this->_driver) {
         $config = Configuration::get("cache");
         $this->driver = $config['default'];
         $this->options = Configuration::get("cache.settings." . $this->_driver);
     }
     if (!$this->_driver) {
         throw new \Exception("Invalid type", 500);
     }
     Event::fire("framework.cache,.initializ.after", array($this->_driver, $this->_options));
     switch ($this->_driver) {
         case "file":
             return new Cache\Driver\File($this->_options);
             break;
         case "memcached":
             return new Cache\Driver\Memcached($this->_options);
             break;
         case "wincache":
             return new Cache\Driver\Wincache($this->_options);
             break;
         default:
             throw new Exception("Ivalid type", 500);
     }
 }
Exemplo n.º 2
0
 /**
  * Load details from configuration file and initialize
  * driver class
  */
 public function initialize()
 {
     Event::fire("framework.session.initialize.before", array($this->_driver, $this->_options));
     if (!$this->_driver) {
         $config = Configuration::get('session');
         $this->driver = $config['driver'];
         unset($config['driver']);
         $this->options = $config;
     }
     if (!$this->_driver) {
         throw new \Exception("Invalid driver", 500);
     }
     Event::fire("framework.session.initialize.after", array($this->_driver, $this->_options));
     switch ($this->_driver) {
         case 'server':
             return new Session\Driver\Server($this->_options);
             break;
         case 'file':
             return new Session\Driver\File($this->_options);
             break;
         default:
             throw new \Exception("Invalid driver", 500);
             break;
     }
 }
Exemplo n.º 3
0
 /**
  * Render a given view using template implementation
  * and pass into it any required data
  * @return string The processed template
  */
 public function render()
 {
     Event::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);
 }
Exemplo n.º 4
0
 public function __construct($options = array())
 {
     parent::__construct($options);
     // Schedule: Update the users activity in the active users table
     Event::add("framework.router.beforehooks.after", function ($name, $parameters) {
         $session = Registry::get('session');
         $user = $session->get('user');
         $time = date("Y-m-d H:i:s");
         if ($user) {
             // @todo REPLACE INTO very Mysql specific build merge method for alternate db
             Database::replace("REPLACE INTO user_active VALUES (:user, :time)", array(':user' => $user, ':time' => $time));
         }
     });
 }
Exemplo n.º 5
0
 /**
  * Create a new database connector instance using configuration
  * defined in application/configuration/Database
  * @return Database\Connectors\Connector
  */
 public static function connection($type = null)
 {
     Event::fire("framework.database.connect.before", array($type));
     if (is_null($type)) {
         $type = Configuration::get('database.default');
     }
     if (!isset(static::$_connections[$type])) {
         $options = Configuration::get('database.connections.' . $type);
         if (is_null($options)) {
             throw new \Exception("Database connection is not defined for " . $type, 500);
         }
         static::$_connections[$type] = new Connection(static::connect($options), $type);
     }
     Event::fire("framework.database.connect.after", array($type));
     return static::$_connections[$type];
 }
Exemplo n.º 6
0
 public function __construct($options = array())
 {
     parent::__construct($options);
     // Schedule: Load user from session
     Event::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(array('id', '=', $user)));
             $acl = new \Acl($user);
             $controller->user->userPerms = $acl->perms;
         }
     });
     // Shedule: Save user to session
     Event::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);
         }
     });
 }
Exemplo n.º 7
0
 /**
  * Call the render method at the end of each action's execution
  */
 public function __destruct()
 {
     Event::fire("framework.done", array("render"));
     Event::fire("framework.controller.destruct.before", array($this->name));
     $this->render();
     Event::fire("framework.controller.destruct.after", array($this->name));
 }
Exemplo n.º 8
0
 /**
  * Load a controller
  * @return boolean|string
  */
 private function _loadController($method, $controller, $action, $parameters = array())
 {
     $name = ucfirst($controller);
     $this->_controller = $controller;
     $this->_action = $action;
     Event::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 {$name} not found", 404);
     }
     Event::fire("framework.router.controller.after", array($controller, $parameters));
     if (!empty($action)) {
         // If request method valid append to action
         if ($this->_validateRequestMethod($method)) {
             $action = strtolower($method) . ucfirst($action);
         }
         // Check if called method exists
         if (!method_exists($instance, $action)) {
             throw new \Exception("Action {$action} not found", 404);
         }
         // Check if method is callable (checking for @protected or @private)
         $inspector = new Inspector($instance);
         $methodMeta = $inspector->getMethodMeta($action);
         if (!empty($methodMeta["@protected"]) || !empty($methodMeta["@private"])) {
             throw new \Exception("Action {$action} not found", 404);
         }
     }
     $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"])) {
                     contiue;
                 }
                 $instance->{$method}();
                 $run[] = $method;
             }
         }
     };
     Event::fire("framework.router.beforehooks.before", array($action, $parameters));
     $hooks($methodMeta, "@before");
     Event::fire("framework.router.beforehooks.after", array($action, $parameters));
     Event::fire("framework.router.action.before", array($action, $parameters));
     // Call Method or resort to default
     switch (true) {
         case !empty($action):
             call_user_func_array(array($instance, $action), is_array($parameters) ? $parameters : array());
             break;
         default:
             call_user_func_array(array($instance, $this->_defaultAction), is_array($parameters) ? $parameters : array());
             break;
     }
     Event::fire("framework.router.action.after", array($action, $parameters));
     Event::fire("framework.router.afterhooks.before", array($action, $parameters));
     $hooks($methodMeta, "@after");
     Event::fire("framework.router.afterhooks.after", array($action, $parameters));
     // unset controller
     Registry::erase("controller");
 }
Exemplo n.º 9
0
 /**
  * Using cURL make a HTTP request
  * @param  string $method     Request method to be used
  * @param  string $url        Target remote url
  * @param  array  $parameters Parameters to be sent to url
  * @return mixed              Response of HTTP request
  */
 public function request($method, $url, $parameters = array())
 {
     Event::fire("framework.request.request.before", array($method, $url, $parameters));
     // Create a new cURL resource instance
     $request = $this->_request = curl_init();
     // Turn parameters into a query string
     if (is_array($parameters)) {
         $parameters = http_build_query($parameters, "", "&");
     }
     // Set the cURL instance parameters and make the request
     $this->_setRequestMethod($method)->_setRequestOptions($url, $parameters)->_setRequestHeaders();
     $response = curl_exc($request);
     if ($response) {
         $response = new Request\Response(array('response' => $response));
     } else {
         throw new \Exception(curl_errno($request) . ' - ' . curl_error($request), 500);
     }
     Event::fire("framework.request.request.after", array($method, $url, $parameters));
     curl_close($request);
     return $response;
 }