lithium\core\Libraries::_search()

protected static method

Search file system.

Parameters

  • string $config
  • array $options
  • string $name

Returns

array

Source

	protected static function _search($config, $options, $name = null) {
		$defaults = [
			'path' => null,
			'suffix' => null,
			'namespaces' => false,
			'recursive' => false,
			'preFilter' => '/[A-Z][A-Za-z0-9]+\./',
			'filter' => false,
			'exclude' => false,
			'format' => function ($file, $config) {
				$trim = [strlen($config['path']) + 1, strlen($config['suffix'])];
				$file = substr($file, $trim[0], -$trim[1]);
				return $config['prefix'] . str_replace('/', '\\', $file);
			}
		];
		$options += $defaults;
		$path = $options['path'];
		$suffix = $options['namespaces'] ? '' : $config['suffix'];
		$suffix = ($options['suffix'] === null) ? $suffix : $options['suffix'];


		$libs = (array) glob($path . $suffix, $options['namespaces'] ? GLOB_ONLYDIR : 0);

		if ($options['recursive']) {
			list($current, $match) = explode('/*', $path, 2);
			$queue = array_diff(
				(array) glob($current . '/*', GLOB_ONLYDIR),
				$libs
			);
			$match = str_replace('##', '.+', preg_quote(str_replace('*', '##', $match), '/'));
			$match = '/' . $match . preg_quote($suffix, '/') . '$/';

			while ($queue) {
				if (!is_dir($dir = array_pop($queue))) {
					continue;
				}
				$libs = array_merge(
					$libs,
					(array) glob("{$dir}/*{$suffix}")
				);
				$queue = array_merge(
					$queue,
					array_diff((array) glob("{$dir}/*", GLOB_ONLYDIR), $libs)
				);
			}
			$libs = preg_grep($match, $libs);
		}
		if ($suffix) {
			$libs = $options['preFilter'] ? preg_grep($options['preFilter'], $libs) : $libs;
		}
		return static::_filter($libs, (array) $config, $options + compact('name'));
	}