Commit: e141ab680db2b9ec7839eab30612c4423f61bf00

Author: Nate Abele | Date: 2010-02-23 07:03:07 -0500
Merging "plugin" and "library" concepts; both are now handled uniformly as "libraries". Removing `app/libraries/plugins` directory, removing plugin loading / configuration code, and refactoring `\core\Libraries::add()`. Adding route auto-loading to filter in `app/config/bootstrap/action.php`.
diff --git a/.gitignore b/.gitignore index 068a06b..43a915a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .DS_Store -app/libraries/plugins/* +app/libraries/* app/resources/tmp/* \ No newline at end of file diff --git a/app/config/bootstrap/action.php b/app/config/bootstrap/action.php index 17542ed..736ede2 100644 --- a/app/config/bootstrap/action.php +++ b/app/config/bootstrap/action.php @@ -17,18 +17,29 @@ * @see lithium\util\collection\Filters */ +use \lithium\core\Libraries; use \lithium\net\http\Router; use \lithium\core\Environment; use \lithium\action\Dispatcher; /** - * Loads application routes before the request is dispatched. Change this to `include_once` if - * more than one request cycle is executed per HTTP request. + * This filter loads all application routes in all plugins, loading the default application routes + * last. Change this code if plugin routes must be loaded in a specific order, or if application + * routes must be loaded first (in which case the catch-all routes should be removed). If + * `Dispatcher::run()` is called multiple times in the course of a single request, change the + * `include`s to `include_once`. * * @see lithium\net\http\Router */ Dispatcher::applyFilter('run', function($self, $params, $chain) { - include __DIR__ . '/../routes.php'; + foreach (Libraries::get() as $name => $config) { + if ($config['default']) { + continue; + } + $file = "{$config['path']}/config/routes.php"; + file_exists($file) ? include $file : null; + } + include LITHIUM_APP_PATH . '/config/routes.php'; return $chain->next($self, $params, $chain); }); diff --git a/app/libraries/empty b/app/libraries/empty new file mode 100644 index 0000000..e69de29 diff --git a/app/libraries/plugins/empty b/app/libraries/plugins/empty deleted file mode 100644 index e69de29..0000000 diff --git a/libraries/lithium/core/Libraries.php b/libraries/lithium/core/Libraries.php index ff103d4..0fdaa6d 100644 --- a/libraries/lithium/core/Libraries.php +++ b/libraries/lithium/core/Libraries.php @@ -83,10 +83,6 @@ class Libraries { '{:root}/libraries/{:name}' ), 'models' => '{:library}\models\{:name}', - 'plugins' => array( - '{:app}/libraries/plugins/{:name}', - '{:root}/libraries/plugins/{:name}' - ), 'test' => array( '{:library}\extensions\test\{:namespace}\{:class}\{:name}', '{:library}\test\{:namespace}\{:class}\{:name}' => array('libraries' => 'lithium') @@ -194,7 +190,7 @@ class Libraries { * which receives the class name as a parameter, and returns a file path as output. * @return array Returns the resulting set of options created for this library. */ - public static function add($name, $config = array()) { + public static function add($name, array $config = array()) { $defaults = array( 'path' => null, 'prefix' => $name . "\\", @@ -202,30 +198,32 @@ class Libraries { 'loader' => null, 'includePath' => false, 'transform' => null, - 'bootstrap' => null, + 'bootstrap' => true, 'defer' => false, 'default' => false ); - if ($name == 'lithium') { - $defaults['path'] = LITHIUM_LIBRARY_PATH . '/lithium'; - $defaults['loader'] = 'lithium\core\Libraries::load'; + if ($name === 'lithium') { $defaults['defer'] = true; - } elseif ($name == 'plugin') { - return static::_addPlugins((array) $config); + $defaults['bootstrap'] = false; + $defaults['path'] = dirname(__DIR__); + $defaults['loader'] = 'lithium\core\Libraries::load'; } - $config = (array) $config + $defaults; - if ($config['default']) { + if (isset($config['default']) && $config['default']) { static::$_default = $name; - $config['path'] = $config['path'] ?: LITHIUM_APP_PATH; + $defaults['path'] = LITHIUM_APP_PATH; + $defaults['bootstrap'] = false; } + $config += $defaults; - if ($config['path']) { - $config['path'] = str_replace('\\', '/', $config['path']); - } else { - $config['path'] = static::locate('libraries', $name); + if (!$config['path']) { + $config['path'] = static::_locatePath('libraries', compact('name') + array( + 'app' => LITHIUM_APP_PATH, 'root' => LITHIUM_LIBRARY_PATH + )); } + + $config['path'] = str_replace('\\', '/', $config['path']); static::$_configurations[$name] = $config; if ($config['includePath']) { @@ -233,10 +231,11 @@ class Libraries { set_include_path(get_include_path() . PATH_SEPARATOR . $path); } - if (!empty($config['bootstrap'])) { - if ($config['bootstrap'] === true) { - $config['bootstrap'] = 'config/bootstrap.php'; - } + if ($config['bootstrap'] === true) { + $path = "{$config['path']}/config/bootstrap.php"; + $config['bootstrap'] = file_exists($path) ? '/config/bootstrap.php' : false; + } + if ($config['bootstrap']) { require "{$config['path']}/{$config['bootstrap']}"; } @@ -685,7 +684,7 @@ class Libraries { if ($exclude = $options['exclude']) { if (is_string($exclude)) { $libs = preg_grep($exclude, $libs, PREG_GREP_INVERT); - } else if (is_callable($exclude)){ + } elseif (is_callable($exclude)) { $libs = array_values(array_filter($libs, $exclude)); } } @@ -700,43 +699,6 @@ class Libraries { } /** - * Register a Lithium plugin. - * - * @param string $plugins - * @param string $options - * @return void - */ - protected static function _addPlugins($plugins) { - $defaults = array('bootstrap' => null, 'route' => true, 'path' => null); - $params = array('app' => LITHIUM_APP_PATH, 'root' => LITHIUM_LIBRARY_PATH); - $result = array(); - - foreach ($plugins as $name => $options) { - if (is_int($name)) { - $name = $options; - $options = array(); - } - $options += $defaults; - - if ($options['path'] === null) { - $options['path'] = static::_locatePath('plugins', compact('name') + $params); - } - if ($options['bootstrap'] === null) { - $options['bootstrap'] = file_exists($options['path'] . '/config/bootstrap.php'); - } - $plugin = static::add($name, $options); - - if ($plugin['route']) { - $defaultRoutes = $plugin['path'] . '/config/routes.php'; - $route = ($plugin['route'] === true) ? $defaultRoutes : $plugin['route']; - !file_exists($route) ?: include $route; - } - $result[$name] = $plugin; - } - return $result; - } - - /** * Get params from type. * * @param string $type