Development

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

You must first sign up to be able to contribute.

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

Revision 7791, 6.0 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  * 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 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     $myConfig = $this->parseYamls($configFiles);
38
39     $myConfig = sfToolkit::arrayDeepMerge(
40       isset($myConfig['default']) && is_array($myConfig['default']) ? $myConfig['default'] : array(),
41       isset($myConfig['all']) && is_array($myConfig['all']) ? $myConfig['all'] : array(),
42       isset($myConfig[sfConfig::get('sf_environment')]) && is_array($myConfig[sfConfig::get('sf_environment')]) ? $myConfig[sfConfig::get('sf_environment')] : array()
43     );
44
45     // init our data and includes arrays
46     $includes  = array();
47     $inits     = array();
48     $instances = array();
49
50     // available list of factories
51     $factories = array('controller', 'request', 'response', 'storage', 'user', 'view_cache');
52
53     // let's do our fancy work
54     foreach ($factories as $factory)
55     {
56       // see if the factory exists for this controller
57       $keys = $myConfig[$factory];
58
59       if (!isset($keys['class']))
60       {
61         // missing class key
62         $error = sprintf('Configuration file "%s" specifies category "%s" with missing class key', $configFiles[0], $factory);
63         throw new sfParseException($error);
64       }
65
66       $class = $keys['class'];
67
68       if (isset($keys['file']))
69       {
70         // we have a file to include
71         $file = $this->replaceConstants($keys['file']);
72         $file = $this->replacePath($file);
73
74         if (!is_readable($file))
75         {
76             // factory file doesn't exist
77             $error = sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s"', $configFiles[0], $class, $file);
78             throw new sfParseException($error);
79         }
80
81         // append our data
82         $includes[] = sprintf("require_once('%s');", $file);
83       }
84
85       // parse parameters
86       if (isset($keys['param']))
87       {
88         $parameters = array();
89         foreach ($keys['param'] as $key => $value)
90         {
91           $parameters[$key] = $this->replaceConstants($value);
92         }
93       }
94       else
95       {
96         $parameters = null;
97       }
98       $parameters = var_export($parameters, true);
99
100       // append new data
101       switch ($factory)
102       {
103         case 'controller':
104           // append instance creation
105           $instances[] = sprintf("  \$this->controller = sfController::newInstance(sfConfig::get('sf_factory_controller', '%s'));", $class);
106
107           // append instance initialization
108           $inits[] = "  \$this->controller->initialize(\$this);";
109           break;
110
111         case 'request':
112           // append instance creation
113           $instances[] = sprintf("  \$this->request = sfRequest::newInstance(sfConfig::get('sf_factory_request', '%s'));", $class);
114
115           // append instance initialization
116           $inits[] = sprintf("  \$this->request->initialize(\$this, sfConfig::get('sf_factory_request_parameters', %s), sfConfig::get('sf_factory_request_attributes', array()));", $parameters);
117           break;
118
119         case 'response':
120           // append instance creation
121           $instances[] = sprintf("  \$this->response = sfResponse::newInstance(sfConfig::get('sf_factory_response', '%s'));", $class);
122
123           // append instance initialization
124           $inits[] = sprintf("  \$this->response->initialize(\$this, sfConfig::get('sf_factory_response_parameters', %s));", $parameters);
125           break;
126
127         case 'storage':
128           // append instance creation
129           $instances[] = sprintf("  \$this->storage = sfStorage::newInstance(sfConfig::get('sf_factory_storage', '%s'));", $class);
130
131           // append instance initialization
132           $inits[] = sprintf("  \$this->storage->initialize(\$this, sfConfig::get('sf_factory_storage_parameters', %s));", $parameters);
133           break;
134
135         case 'user':
136           // append instance creation
137           $instances[] = sprintf("  \$this->user = sfUser::newInstance(sfConfig::get('sf_factory_user', '%s'));", $class);
138
139           // append instance initialization
140           $inits[] = sprintf("  \$this->user->initialize(\$this, sfConfig::get('sf_factory_user_parameters', %s));", $parameters);
141           break;
142         case 'view_cache':
143           // append view cache class name
144           $inits[] = sprintf("\n  if (sfConfig::get('sf_cache'))\n  {\n".
145                              "    \$this->viewCacheManager = new sfViewCacheManager();\n".
146                              "    \$this->viewCacheManager->initialize(\$this, sfConfig::get('sf_factory_view_cache', '%s'), sfConfig::get('sf_factory_view_cache_parameters', %s));\n".
147                              " }\n",
148                              $class, $parameters);
149           break;
150       }
151     }
152
153     // compile data
154     $retval = sprintf("<?php\n".
155                       "// auto-generated by sfFactoryConfigHandler\n".
156                       "// date: %s\n%s\n%s\n%s\n",
157                       date('Y/m/d H:i:s'), implode("\n", $includes),
158                       implode("\n", $instances), implode("\n", $inits));
159
160     return $retval;
161   }
162 }
163
Note: See TracBrowser for help on using the browser.