/** * Loads a plugin and optionally loads bootstrapping, * routing files or runs an initialization function. * * Plugins only need to be loaded if you want bootstrapping/routes/cli commands to * be exposed. If your plugin does not expose any of these features you do not need * to load them. * * This method does not configure any autoloaders. That must be done separately either * through composer, or your own code during config/bootstrap.php. * * ### Examples: * * `Plugin::load('DebugKit')` * * Will load the DebugKit plugin and will not load any bootstrap nor route files. * However, the plugin will be part of the framework default routes, and have its * CLI tools (if any) available for use. * * `Plugin::load('DebugKit', ['bootstrap' => true, 'routes' => true])` * * Will load the bootstrap.php and routes.php files. * * `Plugin::load('DebugKit', ['bootstrap' => false, 'routes' => true])` * * Will load routes.php file but not bootstrap.php * * `Plugin::load('FOC/Authenticate')` * * Will load plugin from `plugins/FOC/Authenticate`. * * It is also possible to load multiple plugins at once. Examples: * * `Plugin::load(['DebugKit', 'ApiGenerator'])` * * Will load the DebugKit and ApiGenerator plugins. * * `Plugin::load(['DebugKit', 'ApiGenerator'], ['bootstrap' => true])` * * Will load bootstrap file for both plugins * * ``` * Plugin::load([ * 'DebugKit' => ['routes' => true], * 'ApiGenerator' * ], * ['bootstrap' => true]) * ``` * * Will only load the bootstrap for ApiGenerator and only the routes for DebugKit * * ### Configuration options * * - `bootstrap` - array - Whether or not you want the $plugin/config/bootstrap.php file loaded. * - `routes` - boolean - Whether or not you want to load the $plugin/config/routes.php file. * - `ignoreMissing` - boolean - Set to true to ignore missing bootstrap/routes files. * - `path` - string - The path the plugin can be found on. If empty the default plugin path (App.pluginPaths) will be used. * - `classBase` - The path relative to `path` which contains the folders with class files. * Defaults to "src". * - `autoload` - boolean - Whether or not you want an autoloader registered. This defaults to false. The framework * assumes you have configured autoloaders using composer. However, if your application source tree is made up of * plugins, this can be a useful option. * * @param string|array $plugin name of the plugin to be loaded in CamelCase format or array or plugins to load * @param array $config configuration options for the plugin * @throws \Cake\Core\Exception\MissingPluginException if the folder for the plugin to be loaded is not found * @return void */ public static function load($plugin, array $config = []) { if (is_array($plugin)) { foreach ($plugin as $name => $conf) { list($name, $conf) = is_numeric($name) ? [$conf, $config] : [$name, $conf]; static::load($name, $conf); } return; } static::_loadConfig(); $config += ['autoload' => false, 'bootstrap' => false, 'routes' => false, 'classBase' => 'src', 'ignoreMissing' => false]; if (!isset($config['path'])) { $config['path'] = Configure::read('plugins.' . $plugin); } if (empty($config['path'])) { $paths = App::path('Plugin'); $pluginPath = str_replace('/', DS, $plugin); foreach ($paths as $path) { if (is_dir($path . $pluginPath)) { $config['path'] = $path . $pluginPath . DS; break; } } } if (empty($config['path'])) { throw new MissingPluginException(['plugin' => $plugin]); } $config['classPath'] = $config['path'] . $config['classBase'] . DS; if (!isset($config['configPath'])) { $config['configPath'] = $config['path'] . 'config' . DS; } static::$_plugins[$plugin] = $config; if ($config['autoload'] === true) { if (empty(static::$_loader)) { static::$_loader = new ClassLoader(); static::$_loader->register(); } static::$_loader->addNamespace(str_replace('/', '\\', $plugin), $config['path'] . $config['classBase'] . DS); static::$_loader->addNamespace(str_replace('/', '\\', $plugin) . '\\Test', $config['path'] . 'tests' . DS); } if ($config['bootstrap'] === true) { static::bootstrap($plugin); } }
/** * Get/set a compatible composer autoloader. * * @param object|null $loader The autoloader to set or `null` to get the default one. * @return object The autoloader. */ public static function loader($loader = null) { if ($loader) { return static::$_loader = $loader; } if (static::$_loader !== null) { return static::$_loader; } $loaders = spl_autoload_functions(); foreach ($loaders as $key => $loader) { if (is_array($loader) && method_exists($loader[0], 'findFile')) { return static::$_loader = $loader[0]; } } }
public static function init() { static::$_loader = (require TESTS_PATH . '/../vendor/autoload.php'); static::$_config = (require TESTS_PATH . '/config.php'); static::$_adapter = new Adapter(array('driver' => 'Mysqli', 'host' => static::$_config['db']['host'], 'database' => static::$_config['db']['dbname'], 'username' => static::$_config['db']['username'], 'password' => static::$_config['db']['password'], 'driver_options' => array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'), 'options' => array('buffer_results' => true))); }
/** * Loads a plugin and optionally loads bootstrapping, * routing files or runs a initialization function. * * Plugins only need to be loaded if you want bootstrapping/routes/cli commands to * be exposed. If your plugin does not expose any of these features you do not need * to load them. * * This method does not configure any autoloaders. That must be done separately either * through composer, or your own code during App/Config/bootstrap.php. * * ## Examples: * * `Plugin::load('DebugKit')` * * Will load the DebugKit plugin and will not load any bootstrap nor route files. * However, the plugin will be part of the framework default routes, and have its * CLI tools (if any) available for use. * * `Plugin::load('DebugKit', ['bootstrap' => true, 'routes' => true])` * * Will load the bootstrap.php and routes.php files. * * `Plugin::load('DebugKit', ['bootstrap' => false, 'routes' => true])` * * Will load routes.php file but not bootstrap.php * * `Plugin::load('DebugKit', ['namespace' => 'Cake\DebugKit'])` * * Will load files on APP/Plugin/Cake/DebugKit/... * * Bootstrap initialization functions can be expressed as a PHP callback type, * including closures. Callbacks will receive two parameters (plugin name, plugin configuration) * * It is also possible to load multiple plugins at once. Examples: * * `Plugin::load(['DebugKit', 'ApiGenerator'])` * * Will load the DebugKit and ApiGenerator plugins. * * `Plugin::load(['DebugKit', 'ApiGenerator'], ['bootstrap' => true])` * * Will load bootstrap file for both plugins * * {{{ * Plugin::load([ * 'DebugKit' => ['routes' => true], * 'ApiGenerator' * ], * ['bootstrap' => true]) * }}} * * Will only load the bootstrap for ApiGenerator and only the routes for DebugKit * * ## Configuration options * * - `bootstrap` - array - Whether or not you want the $plugin/Config/bootstrap.php file loaded. * - `routes` - boolean - Whether or not you want to load the $plugin/Config/routes.php file. * - `namespace` - string - A custom namespace for the plugin. It will default to the plugin name. * - `ignoreMissing` - boolean - Set to true to ignore missing bootstrap/routes files. * - `path` - string - The path the plugin can be found on. If empty the default plugin path (App.pluginPaths) will be used. * - `autoload` - boolean - Whether or not you want an autoloader registered. This defaults to false. The framework * assumes you have configured autoloaders using composer. However, if your application source tree is made up of * plugins, this can be a useful option. * * @param string|array $plugin name of the plugin to be loaded in CamelCase format or array or plugins to load * @param array $config configuration options for the plugin * @throws \Cake\Core\Error\MissingPluginException if the folder for the plugin to be loaded is not found * @return void */ public static function load($plugin, array $config = []) { if (is_array($plugin)) { foreach ($plugin as $name => $conf) { list($name, $conf) = is_numeric($name) ? [$conf, $config] : [$name, $conf]; static::load($name, $conf); } return; } $config += ['autoload' => false, 'bootstrap' => false, 'routes' => false, 'namespace' => $plugin, 'ignoreMissing' => false]; if (empty($config['path'])) { $paths = App::path('Plugin'); foreach ($paths as $path) { $namespacePath = str_replace('\\', DS, $config['namespace']); if (is_dir($path . $plugin)) { $config += ['path' => $path . $plugin . DS]; break; } if ($plugin !== $config['namespace'] && is_dir($path . $namespacePath)) { $config += ['path' => $path . $namespacePath . DS]; break; } } } if (empty($config['path'])) { throw new Error\MissingPluginException(['plugin' => $plugin]); } static::$_plugins[$plugin] = $config; if ($config['bootstrap'] === true) { static::bootstrap($plugin); } if ($config['autoload'] === true) { if (empty(static::$_loader)) { static::$_loader = new ClassLoader(); static::$_loader->register(); } static::$_loader->addNamespace($config['namespace'], $config['path']); } }