Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6
 * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
/**
13
 * sfCompileConfigHandler gathers multiple files and puts them into a single file.
14
 * Upon creation of the new file, all comments and blank lines are removed.
15
 *
16
 * @package    symfony
17
 * @subpackage config
18
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19
 * @author     Sean Kerr <sean@code-box.org>
20
 * @version    SVN: $Id: sfCompileConfigHandler.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
21
 */
22
class sfCompileConfigHandler extends sfYamlConfigHandler
23
{
24
  /**
25
   * Executes this configuration handler.
26
   *
27
   * @param array $configFiles An array of absolute filesystem path to a configuration file
28
   *
29
   * @return string Data to be written to a cache file
30
   *
31
   * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
32
   * @throws sfParseException If a requested configuration file is improperly formatted
33
   */
34
  public function execute($configFiles)
35
  {
36
    // parse the yaml
37
    $config = self::getConfiguration($configFiles);
38
 
39
    // init our data
40
    $data = '';
41
 
42
    // let's do our fancy work
43
    foreach ($config as $file)
44
    {
45
      if (!is_readable($file))
46
      {
47
        // file doesn't exist
48
        throw new sfParseException(sprintf('Configuration file "%s" specifies nonexistent or unreadable file "%s".', $configFiles[0], $file));
49
      }
50
 
51
      $contents = file_get_contents($file);
52
 
53
      // strip comments (not in debug mode)
54
      if (!sfConfig::get('sf_debug'))
55
      {
56
        $contents = sfToolkit::stripComments($contents);
57
      }
58
 
59
      // insert configuration files
60
/*      $contents = preg_replace_callback(array('#(require|include)(_once)?\((sfContext::getInstance\(\)\->getConfigCache\(\)|\$configCache)->checkConfig\(\'config/([^\']+)\'\)\);#m',
61
                                          '#()()(sfContext::getInstance\(\)\->getConfigCache\(\)|\$configCache)->import\(\'config/([^\']+)\'(, false)?\);#m'),
62
                                        array($this, 'insertConfigFileCallback'), $contents);
63
*/
64
      // strip php tags
65
      $contents = sfToolkit::pregtr($contents, array('/^\s*<\?(php)?/m' => '',
66
                                                     '/^\s*\?>/m'       => ''));
67
 
68
      // replace windows and mac format with unix format
69
      $contents = str_replace("\r", "\n", $contents);
70
 
71
      // replace multiple new lines with a single newline
72
      $contents = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $contents);
73
 
74
      // append file data
75
      $data .= "\n".$contents;
76
    }
77
 
78
    // compile data
79
    $retval = sprintf("<?php\n".
80
                      "// auto-generated by sfCompileConfigHandler\n".
81
                      "// date: %s\n%s\n",
82
                      date('Y/m/d H:i:s'), $data);
83
 
84
    return $retval;
85
  }
86
 
87
  /**
88
   * Callback for configuration file insertion in the cache.
89
   *
90
   */
91
  protected function insertConfigFileCallback($matches)
92
  {
93
    $configFile = 'config/'.$matches[4];
94
 
95
    $configCache = sfContext::getInstance()->getConfigCache();
96
    $configCache->checkConfig($configFile);
97
 
98
    $config = "// '$configFile' config file\n".file_get_contents($configCache->getCacheName($configFile));
99
 
100
    return $config;
101
  }
102
 
103
  /**
104
   * @see sfConfigHandler
105
   */
106
  static public function getConfiguration(array $configFiles)
107
  {
108
    $config = array();
109
    foreach ($configFiles as $configFile)
110
    {
111
      $config = array_merge($config, self::parseYaml($configFile));
112
    }
113
 
114
    return self::replacePath(self::replaceConstants($config));
115
  }
116
}