Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Added new plugin hooks before_router_setup, `after_router_setup
Browse files Browse the repository at this point in the history
…`, ``before_response_send``, ``after_response_send``. Also routed application errors to ``Zepto\Router::error()``
  • Loading branch information
hassankhan committed Feb 2, 2014
1 parent 4ce9fab commit 031c870
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
8 changes: 7 additions & 1 deletion library/Zepto/PluginInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
48 changes: 20 additions & 28 deletions library/Zepto/Zepto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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);
}
Expand All @@ -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');
}
Expand Down

0 comments on commit 031c870

Please sign in to comment.