lithium\core\Libraries::find PHP Method

find() public static method

Finds the classes or namespaces belonging to a particular library. _Note_: This method assumes loaded class libraries use a consistent class-to-file naming convention.
public static find ( mixed $library, array $options = [] ) : array
$library mixed The name of a library added to the application with `Libraries::add()`, or `true` to search all libraries.
$options array The options this method accepts: - `'path'` _string_: A physical filesystem path relative to the directory of the library being searched. If provided, only the classes or namespaces within this path will be returned. - `'recursive'` _boolean_: If `true`, recursively searches all directories (namespaces) in the given library. If `false` (the default), only searches the top level of the given path. - `'filter'` _string_: A regular expression applied to a class after it is transformed into a fully-namespaced class name. The default regular expression filters class names based on the [PSR-0](http://groups.google.com/group/php-standards/web/psr-0-final-proposal) PHP 5.3 naming standard. - `'exclude'` _mixed_: Can be either a regular expression of classes/namespaces to exclude, or a PHP callable to be used with `array_filter()`. - `'namespaces'` _boolean_: Indicates whether namespaces should be included in the search results. If `false` (the default), only classes are returned.
return array Returns an array of fully-namespaced class names found in the given library or libraries.
    public static function find($library, array $options = array())
    {
        $format = function ($file, $config) {
            $trim = array(strlen($config['path']) + 1, strlen($config['suffix']));
            $rTrim = strpos($file, $config['suffix']) !== false ? -$trim[1] : 9999;
            $file = preg_split('/[\\/\\\\]/', substr($file, $trim[0], $rTrim));
            return $config['prefix'] . join('\\', $file);
        };
        $defaults = compact('format') + array('path' => '', 'recursive' => false, 'filter' => '/^(\\w+)?(\\\\[a-z0-9_]+)+\\\\[A-Z][a-zA-Z0-9]+$/', 'exclude' => '', 'namespaces' => false);
        $options += $defaults;
        $libs = array();
        if ($options['namespaces'] && $options['filter'] === $defaults['filter']) {
            $options['format'] = function ($class, $config) use($format, $defaults) {
                if (is_dir($class)) {
                    return $format($class, $config);
                }
                if (preg_match($defaults['filter'], $class = $format($class, $config))) {
                    return $class;
                }
            };
            $options['filter'] = false;
        }
        if ($library === true) {
            foreach (static::$_configurations as $library => $config) {
                $libs = array_merge($libs, static::find($library, $options));
            }
            return $libs;
        }
        if (!isset(static::$_configurations[$library])) {
            return null;
        }
        $config = static::$_configurations[$library];
        $options['path'] = "{$config['path']}{$options['path']}/*";
        $libs = static::_search($config, $options);
        return array_values(array_filter($libs));
    }

Usage Example

Example #1
0
 /**
  * Main method.
  *
  * @param string $path Absolute path to file or directory.
  * @return boolean
  */
 public function run()
 {
     $path = $this->request->action;
     if (!($path = realpath($path))) {
         $this->error('Not a valid path.');
         return false;
     }
     if (!($library = $this->_library($path))) {
         $this->error("No library registered for path `{$path}`.");
         return false;
     }
     $classes = Libraries::find($library, array('recursive' => true, 'exclude' => '/tests|resources|webroot|index$|^app\\\\config|^app\\\\views|Exception$/'));
     $tests = array();
     foreach (Group::all() as $test) {
         $class = preg_replace('/(tests\\\\[a-z]+\\\\|Test$)/', null, $test);
         $tests[$class] = $test;
     }
     foreach ($classes as $class) {
         $coverage = null;
         if ($hasTest = isset($tests[$class])) {
             $report = Dispatcher::run($tests[$class], array('reporter' => 'console', 'format' => 'txt', 'filters' => array('Coverage')));
             $coverage = $report->results['filters']['lithium\\test\\filter\\Coverage'];
             $coverage = isset($coverage[$class]) ? $coverage[$class]['percentage'] : null;
         }
         $this->out(sprintf('%10s | %7s | %s', $hasTest ? 'has test' : 'no test', is_numeric($coverage) ? sprintf('%.2f%%', $coverage) : 'n/a', $class));
     }
 }
All Usage Examples Of lithium\core\Libraries::find