Example #1
0
File: App.php Project: Nervie/Beta
 /**
  * Returns an array of objects of the given type.
  *
  * Example usage:
  *
  * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  *
  * `App::objects('Controller');` returns `array('PagesController', 'BlogController');`
  *
  * You can also search only within a plugin's objects by using the plugin dot
  * syntax.
  *
  * `App::objects('MyPlugin.Model');` returns `array('MyPluginPost', 'MyPluginComment');`
  *
  * @param string $type Type of object, i.e. 'Model', 'Controller', 'View/Helper', 'file', 'class' or 'plugin'
  * @param mixed $path Optional Scan only the path given. If null, paths for the chosen type will be used.
  * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  * @return mixed Either false on incorrect / miss.  Or an array of found objects.
  */
 public static function objects($type, $path = null, $cache = true)
 {
     $extension = '/\\.php$/';
     $includeDirectories = false;
     $name = $type;
     if ($type === 'plugin') {
         $type = 'plugins';
     }
     if ($type == 'plugins') {
         $extension = '/.*/';
         $includeDirectories = true;
     }
     list($plugin, $type) = pluginSplit($type);
     if (isset(self::$legacy[$type . 's'])) {
         $type = self::$legacy[$type . 's'];
     }
     if ($type === 'file' && !$path) {
         return false;
     } elseif ($type === 'file') {
         $extension = '/\\.php$/';
         $name = $type . str_replace(DS, '', $path);
     }
     if (empty(self::$__objects) && $cache === true) {
         self::$__objects = Cache::read('object_map', '_cake_core_');
     }
     $cacheLocation = empty($plugin) ? 'app' : $plugin;
     if ($cache !== true || !isset(self::$__objects[$cacheLocation][$name])) {
         $objects = array();
         if (empty($path)) {
             $path = self::path($type, $plugin);
         }
         foreach ((array) $path as $dir) {
             if ($dir != APP && is_dir($dir)) {
                 $files = new RegexIterator(new DirectoryIterator($dir), $extension);
                 foreach ($files as $file) {
                     if (!$file->isDot()) {
                         $isDir = $file->isDir();
                         if ($isDir && $includeDirectories) {
                             $objects[] = basename($file);
                         } elseif (!$includeDirectories && !$isDir) {
                             $objects[] = substr(basename($file), 0, -4);
                         }
                     }
                 }
             }
         }
         if ($type !== 'file') {
             foreach ($objects as $key => $value) {
                 $objects[$key] = Inflector::camelize($value);
             }
         }
         sort($objects);
         if ($plugin) {
             return $objects;
         }
         self::$__objects[$cacheLocation][$name] = $objects;
         if ($cache) {
             self::$_objectCacheChange = true;
         }
     }
     return self::$__objects[$cacheLocation][$name];
 }
Example #2
0
 /**
  * Returns an index of objects of the given type, with the physical path to each object.
  *
  * @param string	$type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
  * @param mixed		$path Optional
  * @return Configure instance
  * @access public
  */
 function objects($type, $path = null, $cache = true)
 {
     $objects = array();
     $extension = false;
     $name = $type;
     if ($type === 'file' && !$path) {
         return false;
     } elseif ($type === 'file') {
         $extension = true;
         $name = $type . str_replace(DS, '', $path);
     }
     if (empty(self::$__objects) && $cache === true) {
         self::$__objects = Cache::read('object_map', '_cake_core_');
     }
     if (empty(self::$__objects) || !isset(self::$__objects[$type]) || $cache !== true) {
         $types = self::$types;
         if (!isset($types[$type])) {
             return false;
         }
         $objects = array();
         if (empty($path)) {
             $path = self::${"{$type}s"};
             if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
                 array_pop($path);
             }
         }
         $items = array();
         foreach ((array) $path as $dir) {
             if ($type === 'file' || $type === 'class' || strpos($dir, $type) !== false) {
                 $items = self::__list($dir, $types[$type]['suffix'], $extension);
                 $objects = array_merge($items, array_diff($objects, $items));
             }
         }
         if ($type !== 'file') {
             foreach ($objects as $key => $value) {
                 $objects[$key] = Inflector::camelize($value);
             }
         }
         if ($cache === true) {
             self::$__cache = true;
         }
         self::$__objects[$name] = $objects;
     }
     return self::$__objects[$name];
 }
Example #3
0
 /**
  * Returns an array of objects of the given type.
  *
  * Example usage:
  *
  * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  *
  * You can also search only within a plugin's objects by using the plugin dot
  * syntax (these objects are not cached):
  * 
  * `App::objects('MyPlugin.model');` returns `array('Post', 'Comment');`
  *
  * @param string $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
  * @param mixed $path Optional Scan only the path given. If null, paths for the chosen
  *   type will be used.
  * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  * @return mixed Either false on incorrect / miss.  Or an array of found objects.
  */
 public static function objects($type, $path = null, $cache = true)
 {
     $objects = array();
     $extension = false;
     list($plugin, $type) = pluginSplit($type);
     $name = $type;
     if ($plugin) {
         $path = Inflector::pluralize($type);
         if ($path == 'helpers') {
             $path = 'views' . DS . $path;
         } elseif ($path == 'behaviors') {
             $path = 'models' . DS . $path;
         } elseif ($path == 'components') {
             $path = 'controllers' . DS . $path;
         }
         $path = self::pluginPath($plugin) . $path;
         $cache = false;
     }
     if ($type === 'file' && !$path) {
         return false;
     } elseif ($type === 'file') {
         $extension = true;
         $name = $type . str_replace(DS, '', $path);
     }
     if (empty(self::$__objects) && $cache === true) {
         self::$__objects = Cache::read('object_map', '_cake_core_');
     }
     if (!isset(self::$__objects[$name]) || $cache !== true) {
         $types = self::$types;
         if (!isset($types[$type])) {
             return false;
         }
         $objects = array();
         if (empty($path)) {
             $path = self::${"{$type}s"};
             if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
                 array_pop($path);
             }
         }
         $items = array();
         foreach ((array) $path as $dir) {
             if ($dir != APP) {
                 $items = self::__list($dir, $types[$type]['suffix'], $extension);
                 $objects = array_merge($items, array_diff($objects, $items));
             }
         }
         if ($type !== 'file') {
             foreach ($objects as $key => $value) {
                 $objects[$key] = Inflector::camelize($value);
             }
         }
         if ($cache === true) {
             self::$__cache = true;
         }
         if ($plugin) {
             return $objects;
         }
         self::$__objects[$name] = $objects;
     }
     return self::$__objects[$name];
 }