Location: PHPKode > scripts > Redstart Templating > Redstart-Templating-master/Redstart/Templating/Path/PathList.php
<?php

namespace Redstart\Templating\Path;

/**
 * Path List 
 *
 * @package Redstart    
 * @subpackage Redstart_Templating_Path
 *
 * @author Hyyan Abo Fakher
 * @since Redstart 1.0
 * @version 1.0
 */
class PathList implements PathListInterface {

    /**
     * Path list
     * @var \SplStack 
     */
    protected $paths = array();

    /**
     * Constrcut New Path List
     * @param array $paths
     * @throws \InvalidArgumentException
     */
    public function __construct(array $paths = array()) {
        $this->clear();
        $this->setPath($paths);
    }

    /**
     * Add directory Path to the path list
     * @param string $path
     * @throws \InvalidArgumentException
     */
    public function addPath($path) {
        if (!is_dir($path)) {
            throw new \InvalidArgumentException(
            'Path List Excpect Directoires Not Files'
            );
        }

        if (!isset($this->paths[$path])) {
            $this->paths[$path] = rtrim(
                            str_replace(array("\\", "/"), DIRECTORY_SEPARATOR, $path)
                            , DIRECTORY_SEPARATOR
                    ) . DIRECTORY_SEPARATOR;
        }
    }

    /**
     * Add Arry Of Paths
     * @param array $paths
     * @throws \InvalidArgumentException
     */
    public function setPath(array $paths) {
        foreach ($paths as $path) {
            $this->addPath($path);
        }
    }

    /**
     * Add Path List
     * @param \Redstart\Templating\Path\PathListInterface $list
     */
    public function addPathList(PathListInterface $list) {
        $this->setPath(iterator_to_array($list->getIterator()));
    }

    /**
     * Clear the path list
     */
    public function clear() {
        $this->paths = array();
    }

    /**
     * Find file
     * @param string $file file's path realtice to once of the sotred dirs in the 
     *                     path list 
     * @return string|boolean false if the file can't be found or the file's full
     *                        path otherwise
     */
    public function find($file) {
        $paths = array_reverse($this->paths);
        foreach ($paths as $path) {
            $full = $path . $file;
            if (file_exists($full)) {
                return $full;
            }
        }
        return false;
    }

    /**
     * Serialize the path list
     * @return string
     */
    public function serialize() {
        return serialize($this->paths);
    }

    /**
     * Unserialize the path lisr
     * @param string $serialized
     */
    public function unserialize($serialized) {
        $this->paths = unserialize($serialized);
    }

    /**
     * Get iterator
     * 
     * <b>Note : </b>>
     * Paths Will will be returned in the same order they were added
     * 
     * @return \ArrayIterator
     */
    public function getIterator() {
        return new \ArrayIterator($this->paths);
    }

}

Return current item: Redstart Templating