lithium\core\Libraries::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.

Parameters

  • mixed $library

    The name of a library added to the application with Libraries::add(), or true to search all libraries.

  • array $options

    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 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.

Returns

array

Returns an array of fully-namespaced class names found in the given library or libraries.

Source

	public static function find($library, array $options = []) {
		$format = function($file, $config) {
			$trim = [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') + [
			'path' => '',
			'recursive' => false,
			'filter' => '/^(\w+)?(\\\\[a-z0-9_]+)+\\\\[A-Z][a-zA-Z0-9]+$/',
			'exclude' => '',
			'namespaces' => false
		];
		$options += $defaults;
		$libs = [];

		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));
	}