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'];


		$dFlags = GLOB_ONLYDIR;
		$zFlags = 0;
		if (strpos($path, '{') !== false) {
			$message  = "Search path `{$path}` relies on brace globbing. ";
			$message .= 'Support for brace globbing in search paths has been deprecated.';
			trigger_error($message, E_USER_DEPRECATED);

			$dFlags |= GLOB_BRACE;
			$zFlags |= GLOB_BRACE;
		}
		$libs = (array) glob($path . $suffix, $options['namespaces'] ? $dFlags : $zFlags);

		if ($options['recursive']) {
			list($current, $match) = explode('/*', $path, 2);
			$queue = array_diff((array) glob($current . '/*', $dFlags), $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}/*", $dFlags), $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'));
	}