Development

/branches/1.1/lib/config/sfFactoryConfigHandler.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/config/sfFactoryConfigHandler.class.php

Revision 11955, 10.0 kB (checked in by fabien, 6 years ago)

[1.1, 1.2] fixed HTTP-Version in sfWebResponse broken under certain circumstances (closes #4578, #2817)

  • 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  * sfFactoryConfigHandler allows you to specify which factory implementation the
14  * system will use.
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 sfFactoryConfigHandler 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 <b>sfConfigurationException</b> If a requested configuration file does not exist or is not readable
32    * @throws <b>sfParseException</b> 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 and includes arrays
40     $includes  = array();
41     $instances = array();
42
43     // available list of factories
44     $factories = array('logger', 'i18n', 'routing', 'controller', 'request', 'response', 'storage', 'user', 'view_cache');
45
46     // let's do our fancy work
47     foreach ($factories as $factory)
48     {
49       // see if the factory exists for this controller
50       $keys = $config[$factory];
51
52       if (!isset($keys['class']))
53       {
54         // missing class key
55         throw new sfParseException(sprintf('Configuration file "%s" specifies category "%s" with missing class key.', $configFiles[0], $factory));
56       }
57
58       $class = $keys['class'];
59
60       if (isset($keys['file']))
61       {
62         // we have a file to include
63         if (!is_readable($keys['file']))
64         {
65           // factory file doesn't exist
66           throw new sfParseException(sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s".', $configFiles[0], $class, $keys['file']));
67         }
68
69         // append our data
70         $includes[] = sprintf("require_once('%s');", $keys['file']);
71       }
72
73       // parse parameters
74       $parameters = array();
75       if (isset($keys['param']))
76       {
77         if (!is_array($keys['param']))
78         {
79           throw new InvalidArgumentException(sprintf('The "param" key for the "%s" factory must be an array (in %s).', $class, $configFiles[0]));
80         }
81
82         $parameters = $keys['param'];
83       }
84
85       // append new data
86       switch ($factory)
87       {
88         case 'controller':
89           $instances[] = sprintf("  \$class = sfConfig::get('sf_factory_controller', '%s');\n   \$this->factories['controller'] = new \$class(\$this);", $class);
90           break;
91
92         case 'request':
93           $instances[] = sprintf("  \$class = sfConfig::get('sf_factory_request', '%s');\n   \$this->factories['request'] = new \$class(\$this->dispatcher, array(), sfConfig::get('sf_factory_request_parameters', %s), sfConfig::get('sf_factory_request_attributes', array()));", $class, var_export($parameters, true));
94           break;
95
96         case 'response':
97           $instances[] = sprintf("  \$class = sfConfig::get('sf_factory_response', '%s');\n  \$this->factories['response'] = new \$class(\$this->dispatcher, sfConfig::get('sf_factory_response_parameters', array_merge(array('http_protocol' => isset(\$_SERVER['SERVER_PROTOCOL']) ? \$_SERVER['SERVER_PROTOCOL'] : null), %s)));", $class, var_export($parameters, true));
98           // TODO: this is a bit ugly, as it only works for sfWebRequest & sfWebResponse combination. see #3397
99           $instances[] = sprintf("  if (\$this->factories['request'] instanceof sfWebRequest \n      && \$this->factories['response'] instanceof sfWebResponse \n      && 'HEAD' == \$this->factories['request']->getMethodName())\n  {  \n    \$this->factories['response']->setHeaderOnly(true);\n  }\n");
100           break;
101
102         case 'storage':
103           $defaultParameters = array();
104           $defaultParameters[] = sprintf("'auto_shutdown' => false, 'session_id' => \$this->getRequest()->getParameter('%s'),", $parameters['session_name']);
105           if (is_subclass_of($class, 'sfDatabaseSessionStorage'))
106           {
107             $defaultParameters[] = sprintf("'database' => \$this->getDatabaseManager()->getDatabase('%s'),", isset($parameters['database']) ? $parameters['database'] : 'default');
108             unset($parameters['database']);
109           }
110
111           $instances[] = sprintf("  \$class = sfConfig::get('sf_factory_storage', '%s');\n  \$this->factories['storage'] = new \$class(array_merge(array(\n%s\n), sfConfig::get('sf_factory_storage_parameters', %s)));", $class, implode("\n", $defaultParameters), var_export($parameters, true));
112           break;
113
114         case 'user':
115           $instances[] = sprintf("  \$class = sfConfig::get('sf_factory_user', '%s');\n  \$this->factories['user'] = new \$class(\$this->dispatcher, \$this->factories['storage'], array_merge(array('auto_shutdown' => false, 'culture' => \$this->factories['request']->getParameter('sf_culture')), sfConfig::get('sf_factory_user_parameters', %s)));", $class, var_export($parameters, true));
116           break;
117
118         case 'view_cache':
119           $instances[] = sprintf("\n  if (sfConfig::get('sf_cache'))\n  {\n".
120                              "    \$class = sfConfig::get('sf_factory_view_cache', '%s');\n".
121                              "    \$cache = new \$class(sfConfig::get('sf_factory_view_cache_parameters', %s));\n".
122                              "    \$this->factories['viewCacheManager'] = new sfViewCacheManager(\$this, \$cache);\n".
123                              "  }\n".
124                              "  else\n".
125                              "  {\n".
126                              "    \$this->factories['viewCacheManager'] = null;\n".
127                              "  }\n",
128                              $class, var_export($parameters, true));
129           break;
130
131         case 'i18n':
132           if (isset($parameters['cache']))
133           {
134             $cache = sprintf("    \$cache = new %s(%s);\n", $parameters['cache']['class'], var_export($parameters['cache']['param'], true));
135             unset($parameters['cache']);
136           }
137           else
138           {
139             $cache = "    \$cache = null;\n";
140           }
141
142           $instances[] = sprintf("\n  if (sfConfig::get('sf_i18n'))\n  {\n".
143                      "    \$class = sfConfig::get('sf_factory_i18n', '%s');\n".
144                      "%s".
145                      "    \$this->factories['i18n'] = new \$class(\$this->configuration, \$cache, %s);\n".
146                      "    sfWidgetFormSchemaFormatter::setTranslationCallable(array(\$this->factories['i18n'], '__'));\n".
147                      "  }\n"
148                      , $class, $cache, var_export($parameters, true)
149                      );
150           break;
151
152         case 'routing':
153           if (isset($parameters['cache']))
154           {
155             $cache = sprintf("    \$cache = new %s(%s);\n", $parameters['cache']['class'], var_export($parameters['cache']['param'], true));
156             unset($parameters['cache']);
157           }
158           else
159           {
160             $cache = "    \$cache = null;\n";
161           }
162
163           $instances[] = sprintf("  \$class = sfConfig::get('sf_factory_routing', '%s');\n  %s\n\$this->factories['routing'] = new \$class(\$this->dispatcher, \$cache, array_merge(array('auto_shutdown' => false), sfConfig::get('sf_factory_routing_parameters', %s)));", $class, $cache, var_export($parameters, true));
164           break;
165
166         case 'logger':
167           $loggers = '';
168           if (isset($parameters['loggers']))
169           {
170             foreach ($parameters['loggers'] as $name => $keys)
171             {
172               if (isset($keys['enabled']) && !$keys['enabled'])
173               {
174                 continue;
175               }
176
177               if (!isset($keys['class']))
178               {
179                 // missing class key
180                 throw new sfParseException(sprintf('Configuration file "%s" specifies logger "%s" with missing class key.', $configFiles[0], $name));
181               }
182
183               $condition = true;
184               if (isset($keys['param']['condition']))
185               {
186                 $condition = $keys['param']['condition'];
187                 unset($keys['param']['condition']);
188               }
189
190               if ($condition)
191               {
192                 // create logger instance
193                 $loggers .= sprintf("\n\$logger = new %s(\$this->dispatcher, array_merge(array('auto_shutdown' => false), %s));\n\$this->factories['logger']->addLogger(\$logger);\n",
194                               $keys['class'],
195                               isset($keys['param']) ? var_export($keys['param'], true) : 'array()'
196                             );
197               }
198             }
199
200             unset($parameters['loggers']);
201           }
202
203           $instances[] = sprintf(
204                          "  \$class = sfConfig::get('sf_factory_logger', '%s');\n  \$this->factories['logger'] = new \$class(\$this->dispatcher, array_merge(array('auto_shutdown' => false), sfConfig::get('sf_factory_logger_parameters', %s)));\n".
205                          "  %s"
206                          , $class, var_export($parameters, true), $loggers);
207           break;
208       }
209     }
210
211     // compile data
212     $retval = sprintf("<?php\n".
213                       "// auto-generated by sfFactoryConfigHandler\n".
214                       "// date: %s\n%s\n%s\n",
215                       date('Y/m/d H:i:s'), implode("\n", $includes),
216                       implode("\n", $instances));
217
218     return $retval;
219   }
220
221   /**
222    * @see sfConfigHandler
223    */
224   static public function getConfiguration(array $configFiles)
225   {
226     $config = self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));
227
228     foreach ($config as $factory => $values)
229     {
230       if (isset($values['file']))
231       {
232         $config[$factory]['file'] = self::replacePath($values['file']);
233       }
234     }
235
236     return $config;
237   }
238 }
239
Note: See TracBrowser for help on using the browser.