Development

/branches/1.0/lib/config/sfCompileConfigHandler.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/config/sfCompileConfigHandler.class.php

Revision 7791, 3.7 kB (checked in by fabien, 7 years ago)

updated Sean Kerr email address

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
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$
21  */
22 class sfCompileConfigHandler extends sfYamlConfigHandler
23 {
24   /**
25    * Executes this configuration handler.
26    *
27    * @param array 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 = array();
38     foreach ($configFiles as $configFile)
39     {
40       $config = array_merge($config, $this->parseYaml($configFile));
41     }
42
43     // init our data
44     $data = '';
45
46     // let's do our fancy work
47     foreach ($config as $file)
48     {
49       $file = $this->replaceConstants($file);
50       $file = $this->replacePath($file);
51
52       if (!is_readable($file))
53       {
54         // file doesn't exist
55         $error = sprintf('Configuration file "%s" specifies nonexistent or unreadable file "%s"', $configFiles[0], $file);
56         throw new sfParseException($error);
57       }
58
59       $contents = file_get_contents($file);
60
61       // strip comments (not in debug mode)
62       if (!sfConfig::get('sf_debug'))
63       {
64         $contents = sfToolkit::stripComments($contents);
65       }
66
67       // insert configuration files
68       $contents = preg_replace_callback(array('#(require|include)(_once)?\((sfConfigCache::getInstance\(\)|\$configCache)->checkConfig\([^_]+sf_app_config_dir_name[^\.]*\.\'/([^\']+)\'\)\);#m',
69                                           '#()()(sfConfigCache::getInstance\(\)|\$configCache)->import\(.sf_app_config_dir_name\.\'/([^\']+)\'(, false)?\);#m'),
70                                         array($this, 'insertConfigFileCallback'), $contents);
71
72       // strip php tags
73       $contents = sfToolkit::pregtr($contents, array('/^\s*<\?(php)?/m' => '',
74                                                      '/^\s*\?>/m'       => ''));
75
76       // replace windows and mac format with unix format
77       $contents = str_replace("\r""\n", $contents);
78
79       // replace multiple new lines with a single newline
80       $contents = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $contents);
81
82       // append file data
83       $data .= "\n".$contents;
84     }
85
86     // compile data
87     $retval = sprintf("<?php\n".
88                       "// auto-generated by sfCompileConfigHandler\n".
89                       "// date: %s\n%s\n",
90                       date('Y/m/d H:i:s'), $data);
91
92     // save current symfony release
93     file_put_contents(sfConfig::get('sf_config_cache_dir').'/VERSION', file_get_contents(sfConfig::get('sf_symfony_lib_dir').'/VERSION'));
94
95     return $retval;
96   }
97
98   /**
99    * Callback for configuration file insertion in the cache.
100    *
101    */
102   protected function insertConfigFileCallback($matches)
103   {
104     $configFile = sfConfig::get('sf_app_config_dir_name').'/'.$matches[4];
105
106     sfConfigCache::getInstance()->checkConfig($configFile);
107
108     $config = "// '$configFile' config file\n".
109               file_get_contents(sfConfigCache::getInstance()->getCacheName($configFile));
110
111     return $config;
112   }
113 }
114
Note: See TracBrowser for help on using the browser.