Файл: MobileCMS-2.7.0-beta/System/Kernel/Config/ConfigDumper.php
Строк: 76
<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace SystemKernelConfig;
/**
 * Description of ConfigDumper
 *
 * @author KpuTuK
 */
class ConfigDumper {
    /**
     * Массив путей к конфиг-файлам
     * @var array
     */
    protected $paths;
    /**
     * Массив имен кешированных файлов
     * @var array;
     */
    protected $names = [];
    /**
     * Создает экземпляр класса у казанным массивом директорий
     * @param array $paths
     */
    public function __construct(array $paths = []) {
        $this->paths = $paths;
    }
    /**
     * 
     * @param type $file
     * @param array $cacheNames
     */
    public function dumpConfig($file, $cacheNames = null) {
        if (null !== $cacheNames) {
            $this->names = $cacheNames;
        }
        file_put_contents(
            APPS.'Cache/ClassCache/ConfigCache.php', 
            $this->createClass($file)
        );
    }
    protected function createClass($file) {
        $time = (new DateTime)->format('D, d M Y H:i:s');
        return <<<EOL
<?php
namespace ApplicationCacheClassCache;
/**
 * Create by ConfigDumper {$time}
 * Кеш настроек
 */
class ConfigCache {
    public function getNames() {
        return [
            {$this->generateNamesFiles($file)}
        ];
    }
    {$this->generateConfigMethods()}  
}
EOL;
    }
    protected function getPath($file) {
        foreach ($this->paths as $path) {
            if (file_exists(ROOT.$path.DIRECTORY_SEPARATOR.$file.'.php')) {
                return ROOT.$path.DIRECTORY_SEPARATOR.$file.'.php';
            }
        }
        throw new ErrorException(sprintf(
            'Файл конфигурации "%s.php" не найден в папках "%s" !', 
            $file, 
            implode('|', $this->paths)
        ));
    }
    protected function generateNamesFiles($file) {
        $this->names[] = $file;
        $write = '';
        foreach ($this->names as $name) {
            $write .= "t'$name',n";
        }
        return $write;
    }
    protected function generateConfigMethods() {
        $write = '';
        foreach ($this->names as $name) {
            $write .= $this->generateConfigMethod($name);
        }
        return $write;
    }
    protected function generateConfigMethod($file) {
        $source = str_replace('<?php', '', file_get_contents($this->getPath($file)));
        return <<<EOL
tpublic function get{$file}() {
tt{$source}
t}
EOL;
    }
}