diff --git a/library/Zepto/PluginInterface.php b/library/Zepto/PluginInterface.php index 868cd2b..9779955 100644 --- a/library/Zepto/PluginInterface.php +++ b/library/Zepto/PluginInterface.php @@ -21,6 +21,12 @@ public function before_file_load(&$content_dir); public function after_file_load(&$content); - public function request_url(&$url); + public function before_router_setup(); + + public function after_router_setup(); + + public function before_response_send(); + + public function after_response_send(); } diff --git a/library/Zepto/Zepto.php b/library/Zepto/Zepto.php index 7b2fefc..6ffcfd6 100644 --- a/library/Zepto/Zepto.php +++ b/library/Zepto/Zepto.php @@ -73,15 +73,6 @@ public function __construct(array $settings = array()) // Get local reference to container $container = $this->container; - // Create application hooks - $container['hooks'] = array( - 'after_plugins_load' => array(), - 'before_config_load' => array(), - 'request_url' => array(), - 'before_file_load' => array(), - 'after_file_load' => array() - ); - $container['request'] = $container->share( function() { return Request::createFromGlobals(); @@ -153,7 +144,13 @@ function() { */ public function run() { - return $this->container['router']->run(); + $this->run_hooks('before_response_send'); + try { + return $this->container['router']->run(); + } catch (\Exception $e) { + $this->container['router']->error($e); + } + $this->run_hooks('after_response_send'); } /** @@ -166,25 +163,17 @@ public function run() public function run_hooks($hook_id, $args = array()) { $container = $this->container; - $hooks = $container['hooks']; // If plugins are disabled, do not run if ($container['plugins_enabled'] === false) { return false; } - $plugins = $container['plugins']; - - // Check if event name exists - if (array_key_exists($hook_id, $hooks) === false) { - throw new \Exception('No such hook exists'); - } - // Send app reference to hooks - $args = array_merge($args, array($this)); + $args = array_merge($args, array($this->container)); // Run hooks associated with that event - foreach ($plugins as $plugin_id => $plugin) { + foreach ($container['plugins'] as $plugin_id => $plugin) { if (is_callable(array($plugin, $hook_id))) { call_user_func_array(array($plugin, $hook_id), $args); } @@ -199,16 +188,19 @@ public function run_hooks($hook_id, $args = array()) */ protected function load_plugins($plugins_dir) { - $container = $this->container; - - if ($container['plugins_enabled'] === true) { - $plugin_loader = $container['plugin_loader']; + if ($this->container['plugins_enabled'] === true) { + $plugin_loader = $this->container['plugin_loader']; // Load plugins from 'plugins' folder - $container['plugins'] = $plugin_loader->load( - $plugins_dir, - array('.php') - ); + try { + $this->container['plugins'] = $plugin_loader->load( + $plugins_dir, + array('.php') + ); + } + catch (\Exception $e) { + $this->container['router']->error($e); + } } $this->run_hooks('after_plugins_load'); }