Просмотр файла cvServiceContainer/cvServiceContainerCompiler.php

Размер файла: 3.21Kb
<?php

/**
 * cvServiceContainerCompiler
 * 
 * Conversion of configurations of containers in the PHP-code 
 * 
 * @package CYBERVILLE
 * @subpackage DI
 * @author Kochergin Nick <[email protected]>, <http://cyberville-project.ru>
 * @version $Id$
 */
class cvServiceContainerCompiler {

    protected $paths = array();
    protected $loaders = array('yaml'=>'cvServiceContainerLoaderFileYaml', 
                               'xml'=>'cvServiceContainerLoaderFileXml');
    protected $dumper = array('php'=>'cvServiceContainerDumperPhp');

    /**
     * Adding paths to find configuration files containers 
     * 
     * @param array $paths A array paths
     */
    public function addPathsForSearch(array $paths) {
        $this->paths = array_merge($this->paths, $paths);
    }
    
    /**
     * Search has created php file
     * 
     * @param string $name File name
     * @return string path to php file (if the file is found) or false (if the file is not found)
     */
    public function SearchPhpFile($name) {
        foreach ($this->paths as $path) {
            if (is_array($result = $this->FileExists($path, $name, array('php'))))
                return $result['path'].'/'.$result['name'].'.'.$result['ext'];
        }
        
        return false;
    }
    
    /**
     * Search configuration file container
     * 
     * @param string $name File name
     * @return string path to php file
     */
    public function SearchFileForCompilation($name) {
        $formats = array_keys($this->loaders);
        foreach ($this->paths as $path) {
            if (is_array($result = $this->FileExists($path, $name, $formats))) {
                return $this->CompilationToPhp($result);
            }
        }
        
        throw new IncorrectPathException('Files to compile is not found.');
    }
    
    /**
     * Find file
     * 
     * @param string $path Folder
     * @param string $name File name without extension
     * @param array $ext Array of possible file extensions
     * @return array with the keys 'path', 'name', 'ext' (if the file is found) or false (if the file is not found)
     */
    public function FileExists($path, $name, array $ext) {
        foreach ($ext as $e) {
            if (file_exists($path.'/'.$name.'.'.$e))
                return array(
                            'path'=>$path,
                            'name'=>$name,
                            'ext' =>$e
                            );
        }
        
        return false;
    }
    
    /**
     * Compiling
     * 
     * @param array $file_info with the keys 'path', 'name', 'ext'
     * @return string path to php file
     */
    public function CompilationToPhp($file_info) {
        $sc = new cvServiceContainerDefinitions();
        $loader = new $this->loaders[$file_info['ext']]($sc);
        $loader->load($file_info['path'].'/'.$file_info['name'].'.'.$file_info['ext']);
        $dumper = new $this->dumper['php']($sc);
        file_put_contents($pathPhp = $file_info['path'].'/'.$file_info['name'].'.php', $dumper->dump(array('class'=>$file_info['name'])));
        return $pathPhp;
    }

}

class IncorrectPathException extends Exception {}

?>