/** * Override startup of the Shell * * @return void */ public function startup() { parent::startup(); if (isset($this->params['connection'])) { $this->connection = $this->params['connection']; } $class = Configure::read('Acl.classname'); $className = App::classname('Acl.' . $class, 'Adapter'); if ($class !== 'DbAcl' && !is_subclass_of($className, 'Acl\\Adapter\\DbAcl')) { $out = "--------------------------------------------------\n"; $out .= __d('cake_acl', 'Error: Your current CakePHP configuration is set to an ACL implementation other than DB.') . "\n"; $out .= __d('cake_acl', 'Please change your core config to reflect your decision to use DbAcl before attempting to use this script') . "\n"; $out .= "--------------------------------------------------\n"; $out .= __d('cake_acl', 'Current ACL Classname: %s', $class) . "\n"; $out .= "--------------------------------------------------\n"; $this->err($out); return $this->_stop(); } if ($this->command) { if (Configure::check('Datasource') === null) { $this->out(__d('cake_acl', 'Your database configuration was not found. Take a moment to create one.')); $this->args = null; return $this->DbConfig->execute(); } if (!in_array($this->command, ['initdb'])) { $registry = new ComponentRegistry(); $this->Acl = new AclComponent($registry); $controller = new Controller(); } } }
/** * Gets controller to use, either plugin or application controller. * * @param \Cake\Network\Request $request Request object * @param \Cake\Network\Response $response Response for the controller. * @return \Cake\Controller\Controller|false Object if loaded, boolean false otherwise. */ protected function _getController($request, $response) { $pluginPath = $controller = null; $namespace = 'Controller'; if (!empty($request->params['plugin'])) { $pluginPath = $request->params['plugin'] . '.'; } if (!empty($request->params['controller'])) { $controller = $request->params['controller']; } if (!empty($request->params['prefix'])) { $namespace .= '/' . $request->params['prefix']; } $firstChar = substr($controller, 0, 1); if (strpos($controller, '\\') !== false || strpos($controller, '.') !== false || $firstChar === strtolower($firstChar)) { return false; } $className = false; if ($pluginPath . $controller) { $className = App::classname($pluginPath . $controller, $namespace, 'Controller'); } if (!$className) { return false; } $reflection = new ReflectionClass($className); if ($reflection->isAbstract() || $reflection->isInterface()) { return false; } return $reflection->newInstance($request, $response, $controller); }
/** * Find the paths to all the installed shell templates in the app. * * Bake templates are directories under `Template/Bake` path. * They are listed in this order: app -> plugin -> default * * @return array Array of bake templates that are installed. */ protected function _findTemplates() { $paths = App::path('Template'); $plugins = Plugin::loaded(); foreach ($plugins as $plugin) { $paths[] = Plugin::classPath($plugin) . 'Template' . DS; } $core = current(App::core('Template')); $Folder = new Folder($core . 'Bake' . DS . 'default'); $contents = $Folder->read(); $templateFolders = $contents[0]; $paths[] = $core; foreach ($paths as $i => $path) { $paths[$i] = rtrim($path, DS) . DS; } $this->_io->verbose('Found the following bake templates:'); $templates = []; foreach ($paths as $path) { $Folder = new Folder($path . 'Bake', false); $contents = $Folder->read(); $subDirs = $contents[0]; foreach ($subDirs as $dir) { $Folder = new Folder($path . 'Bake' . DS . $dir); $contents = $Folder->read(); $subDirs = $contents[0]; if (array_intersect($contents[0], $templateFolders)) { $templateDir = $path . 'Bake' . DS . $dir . DS; $templates[$dir] = $templateDir; $this->_io->verbose(sprintf("- %s -> %s", $dir, $templateDir)); } } } return $templates; }
/** * Installing plugin * @param null $zipPath * @return array|bool * @throws CakeException */ public function install($zipPath = null) { if (!file_exists($zipPath)) { throw new Exception(__d('spider', 'Invalid plugin file path')); } $pluginInfo = $this->getPluginMeta($zipPath); $pluginHomeDir = App::path('Plugin'); $pluginHomeDir = reset($pluginHomeDir); $pluginPath = $pluginHomeDir . $pluginInfo->name . DS; if (is_dir($pluginPath)) { throw new Exception(__d('spider', 'Plugin already exists')); } $Zip = new \ZipArchive(); if ($Zip->open($zipPath) === true) { new Folder($pluginPath, true); $Zip->extractTo($pluginPath); if (!empty($pluginInfo->rootPath)) { $old = $pluginPath . $pluginInfo->rootPath; $new = $pluginPath; $Folder = new Folder($old); $Folder->move($new); } $Zip->close(); return (array) $pluginInfo; } else { throw new CakeException(__d('spider', 'Failed to extract plugin')); } return false; }
/** * Get controller to use, either plugin controller or application controller * * @param \Cake\Network\Request $request Request object * @param \Cake\Network\Response $response Response for the controller. * @return mixed name of controller if not loaded, or object if loaded */ protected function _getController($request, $response) { $pluginPath = $controller = null; $namespace = 'Controller'; if (!empty($request->params['plugin'])) { $pluginPath = $request->params['plugin'] . '.'; } if (!empty($request->params['controller'])) { $controller = $request->params['controller']; } if (!empty($request->params['prefix'])) { $prefixes = array_map('Cake\\Utility\\Inflector::camelize', explode('/', $request->params['prefix'])); $namespace .= '/' . implode('/', $prefixes); } if (strpos($controller, '\\') !== false || strpos($controller, '.') !== false) { return false; } $className = false; if ($pluginPath . $controller) { $className = App::classname($pluginPath . $controller, $namespace, 'Controller'); } if (!$className) { return false; } $reflection = new ReflectionClass($className); if ($reflection->isAbstract() || $reflection->isInterface()) { return false; } return $reflection->newInstance($request, $response, $controller); }
/** * Get controller to use, either plugin controller or application controller * * @param \Cake\Network\Request $request Request object * @param \Cake\Network\Response $response Response for the controller. * @return mixed name of controller if not loaded, or object if loaded */ protected function _getController($request, $response) { $pluginPath = $controller = null; $namespace = 'Controller'; if (!empty($request->params['plugin'])) { $pluginPath = $request->params['plugin'] . '.'; } if (!empty($request->params['controller'])) { $controller = $request->params['controller']; } if (!empty($request->params['prefix'])) { $namespace .= '/' . Inflector::camelize($request->params['prefix']); } $className = false; if ($pluginPath . $controller) { $className = App::classname($pluginPath . $controller, $namespace, 'Controller'); } if (!$className) { return false; } $reflection = new \ReflectionClass($className); if ($reflection->isAbstract() || $reflection->isInterface()) { return false; } return $reflection->newInstance($request, $response); }
/** * Create a controller for a given request/response * * @param \Cake\Network\Request $request The request to build a controller for. * @param \Cake\Network\Response $response The response to use. * @return \Cake\Controller\Controller */ public function create(Request $request, Response $response) { $pluginPath = $controller = null; $namespace = 'Controller'; if (isset($request->params['plugin'])) { $pluginPath = $request->params['plugin'] . '.'; } if (isset($request->params['controller'])) { $controller = $request->params['controller']; } if (isset($request->params['prefix'])) { if (strpos($request->params['prefix'], '/') === false) { $namespace .= '/' . Inflector::camelize($request->params['prefix']); } else { $prefixes = array_map('Cake\\Utility\\Inflector::camelize', explode('/', $request->params['prefix'])); $namespace .= '/' . implode('/', $prefixes); } } $firstChar = substr($controller, 0, 1); if (strpos($controller, '\\') !== false || strpos($controller, '.') !== false || $firstChar === strtolower($firstChar)) { return $this->missingController($request); } $className = false; if ($pluginPath . $controller) { $className = App::classname($pluginPath . $controller, $namespace, 'Controller'); } if (!$className) { return $this->missingController($request); } $reflection = new ReflectionClass($className); if ($reflection->isAbstract() || $reflection->isInterface()) { return $this->missingController($request); } return $reflection->newInstance($request, $response, $controller); }
/** * Overwrite shell initialize to dynamically load all Queue Related Tasks. * * @return void */ public function initialize() { $plugins = Plugin::loaded(); foreach ($plugins as $plugin) { $pluginPaths = App::path('Shell/Task', $plugin); foreach ($pluginPaths as $pluginPath) { $Folder = new Folder($pluginPath); $res = $Folder->find('Queue.+Task\\.php'); foreach ($res as &$r) { $r = $plugin . '.' . basename($r, 'Task.php'); } $this->tasks = array_merge($this->tasks, $res); } } $paths = App::path('Shell/Task'); foreach ($paths as $path) { $Folder = new Folder($path); $res = array_merge($this->tasks, $Folder->find('Queue.+\\.php')); foreach ($res as &$r) { $r = basename($r, 'Task.php'); } $this->tasks = $res; } parent::initialize(); $this->QueuedTasks->initConfig(); }
/** * Sets up the configuration for the model, and loads ACL models if they haven't been already * * @param Table $model Table instance being attached * @param array $config Configuration * @return void */ public function __construct(Table $model, array $config = []) { $this->_table = $model; if (isset($config[0])) { $config['type'] = $config[0]; unset($config[0]); } if (isset($config['type'])) { $config['type'] = strtolower($config['type']); } parent::__construct($model, $config); $types = $this->_typeMaps[$this->config()['type']]; if (!is_array($types)) { $types = [$types]; } foreach ($types as $type) { $alias = Inflector::pluralize($type); $className = App::className($alias . 'Table', 'Model/Table'); if ($className == false) { $className = App::className('Acl.' . $alias . 'Table', 'Model/Table'); } $config = []; if (!TableRegistry::exists($alias)) { $config = ['className' => $className]; } $model->hasMany($type, ['targetTable' => TableRegistry::get($alias, $config)]); } if (!method_exists($model->entityClass(), 'parentNode')) { trigger_error(__d('cake_dev', 'Callback {0} not defined in {1}', ['parentNode()', $model->entityClass()]), E_USER_WARNING); } }
/** * Resolve a driver classname. * * Part of the template method for Cake\Core\ObjectRegistry::load() * * @param string $class Partial classname to resolve. * @return string|false Either the correct classname or false. */ protected function _resolveClassName($class) { if (is_object($class)) { return $class; } return App::className($class, 'Datasource'); }
/** * Renders the given cell. * * Example: * * ``` * // Taxonomy\View\Cell\TagCloudCell::smallList() * $cell = $this->cell('Taxonomy.TagCloud::smallList', ['limit' => 10]); * * // App\View\Cell\TagCloudCell::smallList() * $cell = $this->cell('TagCloud::smallList', ['limit' => 10]); * ``` * * The `display` action will be used by default when no action is provided: * * ``` * // Taxonomy\View\Cell\TagCloudCell::display() * $cell = $this->cell('Taxonomy.TagCloud'); * ``` * * Cells are not rendered until they are echoed. * * @param string $cell You must indicate cell name, and optionally a cell action. e.g.: `TagCloud::smallList` * will invoke `View\Cell\TagCloudCell::smallList()`, `display` action will be invoked by default when none is provided. * @param array $data Additional arguments for cell method. e.g.: * `cell('TagCloud::smallList', ['a1' => 'v1', 'a2' => 'v2'])` maps to `View\Cell\TagCloud::smallList(v1, v2)` * @param array $options Options for Cell's constructor * @return \Cake\View\Cell The cell instance * @throws \Cake\View\Exception\MissingCellException If Cell class was not found. * @throws \BadMethodCallException If Cell class does not specified cell action. */ public function cell($cell, array $data = [], array $options = []) { $parts = explode('::', $cell); if (count($parts) === 2) { list($pluginAndCell, $action) = [$parts[0], $parts[1]]; } else { list($pluginAndCell, $action) = [$parts[0], 'display']; } list($plugin) = pluginSplit($pluginAndCell); $className = App::className($pluginAndCell, 'View/Cell', 'Cell'); if (!$className) { throw new Exception\MissingCellException(['className' => $pluginAndCell . 'Cell']); } $cell = $this->_createCell($className, $action, $plugin, $options); if (!empty($data)) { $data = array_values($data); } try { $reflect = new ReflectionMethod($cell, $action); $reflect->invokeArgs($cell, $data); return $cell; } catch (ReflectionException $e) { throw new BadMethodCallException(sprintf('Class %s does not have a "%s" method.', $className, $action)); } }
/** * Renders the given gizmo. * * Example: * * {{{ * // Taxonomy\View\Gizmo\TagCloudGizmo::smallList() * $gizmo = $this->gizmo('Taxonomy.TagCloud::smallList', ['limit' => 10]); * * // App\View\Gizmo\TagCloudGizmo::smallList() * $gizmo = $this->gizmo('TagCloud::smallList', ['limit' => 10]); * }}} * * The `display` action will be used by default when no action is provided: * * {{{ * // Taxonomy\View\Gizmo\TagCloudGizmo::display() * $gizmo = $this->gizmo('Taxonomy.TagCloud'); * }}} * * Gizmos are not rendered until they are echoed. * * @param string $gizmo You must indicate gizmo name, and optionally a gizmo action. e.g.: `TagCloud::smallList` * will invoke `View\Gizmo\TagCloudGizmo::smallList()`, `display` action will be invoked by default when none is provided. * @param array $data Additional arguments for gizmo method. e.g.: * `gizmo('TagCloud::smallList', ['a1' => 'v1', 'a2' => 'v2'])` maps to `View\Gizmo\TagCloud::smallList(v1, v2)` * @param array $options Options for Gizmo's constructor * @return \Cake\View\Gizmo The gizmo instance * @throws \Cake\View\Exception\MissingGizmoException If Gizmo class was not found. * @throws \BadMethodCallException If Gizmo class does not specified gizmo action. */ public function gizmo($gizmo, array $data = [], array $options = []) { $parts = explode('::', $gizmo); if (count($parts) === 2) { list($pluginAndGizmo, $action) = [$parts[0], $parts[1]]; } else { list($pluginAndGizmo, $action) = [$parts[0], 'display']; } list($plugin, $gizmoName) = pluginSplit($pluginAndGizmo); $className = App::className($pluginAndGizmo, 'View/Gizmo', 'Gizmo'); if (!$className) { throw new Exception\MissingGizmoException(array('className' => $pluginAndGizmo . 'Gizmo')); } $gizmo = $this->_createGizmo($className, $action, $plugin, $options); if (!empty($data)) { $data = array_values($data); } try { $reflect = new \ReflectionMethod($gizmo, $action); $reflect->invokeArgs($gizmo, $data); return $gizmo; } catch (\ReflectionException $e) { throw new \BadMethodCallException(sprintf('Class %s does not have a "%s" method.', $className, $action)); } }
/** * Get controller to use, either plugin controller or application controller * * @param \Cake\Network\Request $request Request object * @param \Cake\Network\Response $response Response for the controller. * @return mixed name of controller if not loaded, or object if loaded */ protected function _getController($request, $response) { $pluginPath = $controller = null; $namespace = 'Controller'; if (!empty($request->params['plugin'])) { $pluginPath = $request->params['plugin'] . '.'; } if ($pluginPath) { return parent::_getController($request, $response); } if (!empty($request->params['controller'])) { $controller = $request->params['controller']; } if (!empty($request->params['prefix'])) { $namespace .= '/' . Inflector::camelize($request->params['prefix']); } $className = false; if ($pluginPath . $controller) { $className = App::classname($pluginPath . $controller, $namespace, 'Controller'); } if (!$className) { return false; } $instance = PipingBag::get($className); if (method_exists($instance, 'viewBuilder')) { $instance->viewBuilder(); } else { $instance->viewPath = null; } $instance->name = $controller; $instance->setRequest($request); $instance->response = $response; return $instance; }
/** * Get/Create an instance from the registry. * * When getting an instance, if it does not already exist, * a new instance will be created using the provide alias, and options. * * @param string $alias The name of the alias to get. * @param array $options Configuration options for the type constructor. * @return \Cake\ElasticSearch\Type */ public static function get($alias, array $options = []) { if (isset(static::$instances[$alias])) { if (!empty($options) && static::$options[$alias] !== $options) { throw new RuntimeException(sprintf('You cannot configure "%s", it already exists in the registry.', $alias)); } return static::$instances[$alias]; } static::$options[$alias] = $options; list(, $classAlias) = pluginSplit($alias); $options = $options + ['name' => Inflector::underscore($classAlias)]; if (empty($options['className'])) { $options['className'] = Inflector::camelize($alias); } $className = App::className($options['className'], 'Model/Type', 'Type'); if ($className) { $options['className'] = $className; } else { if (!isset($options['name']) && strpos($options['className'], '\\') === false) { list(, $name) = pluginSplit($options['className']); $options['name'] = Inflector::underscore($name); } $options['className'] = 'Cake\\ElasticSearch\\Type'; } if (empty($options['connection'])) { $connectionName = $options['className']::defaultConnectionName(); $options['connection'] = ConnectionManager::get($connectionName); } static::$instances[$alias] = new $options['className']($options); return static::$instances[$alias]; }
/** * Resolve a cache engine classname. * * Part of the template method for Cake\Core\ObjectRegistry::load() * * @param string $class Partial classname to resolve. * @return string|false Either the correct classname or false. */ protected function _resolveClassName($class) { if (is_object($class)) { return $class; } return App::className($class, 'Cache/Engine', 'Engine'); }
/** * Resolve a behavior classname. * * Part of the template method for Cake\Core\ObjectRegistry::load() * * @param string $class Partial classname to resolve. * @return string|false Either the correct classname or false. */ protected function _resolveClassName($class) { $result = App::className($class, 'Model/Behavior', 'Behavior'); if (!$result) { $result = App::className($class, 'ORM/Behavior', 'Behavior'); } return $result; }
/** * Generates instance of the Transformer. * * The method works with the App::className() method. * This means that you can call app-related Transformers like `Books`. * Plugin-related Transformers can be called like `Plugin.Books` * * @param $className * @return TransformerAbstract * @throws MissingTransformerException */ public function getTransformer($className) { $transformer = App::className(Inflector::classify($className), 'Transformer', 'Transformer'); if ($transformer === false) { throw new MissingTransformerException(['transformer' => $className]); } return new $transformer(); }
/** * generate Encount Sender * * @access private * @author sakuragawa */ private function generateSender($name) { $class = App::className($name, 'Sender'); if (!class_exists($class)) { throw new InvalidArgumentException(sprintf('Encount sender "%s" was not found.', $class)); } return new $class(); }
/** * Resolve a storage class name. * * @param string $class Partial class name to resolve. * @return string The resolved class name. * @throws \Cake\Core\Exception\Exception */ protected function _resolveClassName($class) { $className = App::className($class, 'Model/Storage', 'Storage'); if (!$className) { throw new Exception(sprintf('Storage class "%s" was not found.', $class)); } return $className; }
/** * Return all templates for a given plugin. * * @param string $plugin The plugin to find all templates for. * * @return mixed */ public static function plugin($plugin) { $templates = []; foreach (App::path('Template', $plugin) as $path) { $templates = array_merge($templates, static::iteratePath($path)); } return $templates; }
/** * Resolve a filter class name. * * Part of the template method for Cake\Core\ObjectRegistry::load() * * @param string $class Partial class name to resolve. * @return string|false Either the correct class name or false. */ protected function _resolveClassName($class) { $result = App::className($class, 'Model/Filter', 'Filter'); if ($result || strpos($class, '.') !== false) { return $result; } return App::className('PlumSearch.' . $class, 'Model/Filter', 'Filter'); }
/** * Create an instance of a given classname. * * The config is ignore on susequent requests for the same object * name. To reconfigure an existing object, remove() it and re-get(). * * @param string $class The class to build. * @param array $config The constructor configs to pass to the object. * @return mixed */ public static function get($class, array $config = null) { if (!isset(static::$_instances[$class])) { $className = App::className($class, 'Lib', ''); static::$_instances[$class] = new $className($config); } return static::$_instances[$class]; }
/** * {@inheritDoc} * * @param array $config Configuration * @return void */ public function initialize(array $config) { $this->alias('Permissions'); $this->table('aros_acos'); $this->belongsTo('Aros', ['className' => App::className('Acl.ArosTable', 'Model/Table')]); $this->belongsTo('Acos', ['className' => App::className('Acl.AcosTable', 'Model/Table')]); $this->Aro = $this->Aros->target(); $this->Aco = $this->Acos->target(); }
/** * Create an instance of a filter. * * @param string $name The name of the filter to build. * @param array $options Constructor arguments/options for the filter. * @return \Cake\Routing\DispatcherFilter * @throws \Cake\Routing\Exception\MissingDispatcherFilterException When filters cannot be found. */ protected static function _createFilter($name, $options) { $className = App::className($name, 'Routing/Filter', 'Filter'); if (!$className) { $msg = sprintf('Cannot locate dispatcher filter named "%s".', $name); throw new MissingDispatcherFilterException($msg); } return new $className($options); }
/** * Create a single filter * * @param string $name The name of the filter to build. * @param array $config The configuration for the filter. * @return \MiniAsset\Filter\FilterInterface */ protected function buildFilter($name, $config) { $className = App::className($name, 'Filter'); if (!class_exists($className)) { $className = App::className('AssetCompress.' . $name, 'Filter'); } $className = $className ?: $name; return parent::buildFilter($className, $config); }
/** * {@inheritDoc} * * @param array $config Config * @return void */ public function initialize(array $config) { parent::initialize($config); $this->alias('Aros'); $this->table('aros'); $this->addBehavior('Tree', ['type' => 'nested']); $this->belongsToMany('Acos', ['through' => App::className('Acl.PermissionsTable', 'Model/Table'), 'className' => App::className('Acl.AcosTable', 'Model/Table')]); $this->hasMany('AroChildren', ['className' => App::className('Acl.ArosTable', 'Model/Table'), 'foreignKey' => 'parent_id']); $this->entityClass('Acl.Aro', 'Model/Entity'); }
/** * Constructor * */ public function __construct() { $config = []; if (!TableRegistry::exists('Permissions')) { $config = ['className' => App::className('Acl.PermissionsTable', 'Model/Table')]; } $this->Permission = TableRegistry::get('Permissions', $config); $this->Aro = $this->Permission->Aros->target(); $this->Aco = $this->Permission->Acos->target(); }
/** * Default Constructor * * ### Settings: * * - `engine` Class name to use to replace Cake\I18n\Number functionality * The class needs to be placed in the `Utility` directory. * * @param \Cake\View\View $View The View this helper is being attached to. * @param array $config Configuration settings for the helper * @throws \Cake\Core\Exception\Exception When the engine class could not be found. */ public function __construct(View $View, array $config = []) { parent::__construct($View, $config); $config = $this->_config; $engineClass = App::className($config['engine'], 'Utility'); if ($engineClass) { $this->_engine = new $engineClass($config); } else { throw new Exception(sprintf('Class for %s could not be found', $config['engine'])); } }
/** * Returns a mailer instance. * * @param string $name Mailer's name. * @param \Cake\Mailer\Email|null $email Email instance. * @return \Cake\Mailer\Mailer * @throws \Cake\Mailer\Exception\MissingMailerException if undefined mailer class. */ public function getMailer($name, Email $email = null) { if ($email === null) { $email = new Email(); } $className = App::className($name, 'Mailer', 'Mailer'); if (empty($className)) { throw new MissingMailerException(compact('name')); } return new $className($email); }
/** * Strip the absolute path of template's paths. * * @param array $paths Paths to strip. * @param string|null $plugin Hold plugin name or null for App. * * @return array */ protected static function stripAbsolutePath(array $paths, $plugin = null) { foreach (App::path('Template', $plugin) as $templatesPath) { array_walk($paths, function (&$path) use($templatesPath) { if (substr($path, 0, strlen($templatesPath)) == $templatesPath) { $path = substr($path, strlen($templatesPath)); } }); } return $paths; }