예제 #1
0
	/**
	 * Searches for matching files
	 *
	 * @param Iterator $file
	 * @param string $segment
	 * @param array $segments
	 * @param string $extension
	 * @param Atomik_Model_Builder $builder
	 * @return array
	 */
	protected function _search($file, $segment, $segments, $extension, $builder)
	{
		$files = array();
		
		if (!$file->isDir() && count($segments) == 0) {
			// not a directory
			
			$filename = $file->getFilename();
			$fileExt = null;
			
			if ($extension !== null) {
				// extracting the extension from the filename
				$fileExt = strtolower(substr($filename, strrpos($filename, '.') + 1));
				$filename = substr($filename, 0, strrpos($filename, '.'));
			}
			
			// to match, the segment can either be a variable or match the filename (without the extension)
			// the file extension must matched the searched extensions
			if ((substr($segment, 0, 1) == ':' || $filename == $segment) && $fileExt == $extension) {
				$files[] = $this->_getDataFromFile($file->getPathname(), $builder);
			}
			
		} else if ($file->isDir()) {
			// it is a directory
			
			// the segment is not a variable and the filename does not match
			if (substr($segment, 0, 1) != ':' && $file->getFilename() != $segment) {
				return array();
			}
			
			// the segment is either a variable or it matches the current filename
			// checking in sub files
			$segment = array_shift($segments);
			foreach (new DirectoryIterator($file->getPathname()) as $subFile) {
				if (!$file->isDot()) {
					$files = array_merge($files, $this->_search($subFile, $segment, $segments, $extension, $builder));
				}
			}
		}
		
		return $files;
	}