Development

/branches/1.1/lib/util/sfContext.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/util/sfContext.class.php

Revision 16165, 13.0 kB (checked in by fabien, 6 years ago)

[1.0, 1.1, 1.2, 1.3] added some missing PHPDoc (closes #6054)

  • 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  * sfContext provides information about the current application context, such as
14  * the module and action names and the module directory. References to the
15  * main symfony instances are also provided.
16  *
17  * @package    symfony
18  * @subpackage util
19  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20  * @author     Sean Kerr <sean@code-box.org>
21  * @version    SVN: $Id$
22  */
23 class sfContext
24 {
25   protected
26     $dispatcher    = null,
27     $configuration = null,
28     $factories     = array();
29
30   protected static
31     $instances = array(),
32     $current   = 'default';
33
34   /**
35    * Creates a new context instance.
36    *
37    * @param  sfApplicationConfiguration $configuration  An sfApplicationConfiguration instance
38    * @param  string                     $name           A name for this context (application name by default)
39    * @param  string                     $class          The context class to use (sfContext by default)
40    *
41    * @return sfContext                  An sfContext instance
42    */
43   static public function createInstance(sfApplicationConfiguration $configuration, $name = null, $class = __CLASS__)
44   {
45     if (is_null($name))
46     {
47       $name = $configuration->getApplication();
48     }
49
50     self::$current = $name;
51
52     self::$instances[$name] = new $class();
53
54     if (!self::$instances[$name] instanceof sfContext)
55     {
56       throw new sfFactoryException(sprintf('Class "%s" is not of the type sfContext.', $class));
57     }
58
59     self::$instances[$name]->initialize($configuration);
60
61     return self::$instances[$name];
62   }
63
64   /**
65    * Initializes the current sfContext instance.
66    *
67    * @param sfApplicationConfiguration $configuration  An sfApplicationConfiguration instance
68    */
69   public function initialize(sfApplicationConfiguration $configuration)
70   {
71     $this->configuration = $configuration;
72     $this->dispatcher    = $configuration->getEventDispatcher();
73
74     try
75     {
76       $this->loadFactories();
77     }
78     catch (sfException $e)
79     {
80       $e->printStackTrace();
81     }
82     catch (Exception $e)
83     {
84       sfException::createFromException($e)->printStackTrace();
85     }
86
87     if (sfConfig::get('sf_logging_enabled'))
88     {
89       $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Initialization')));
90     }
91
92     $this->dispatcher->connect('template.filter_parameters', array($this, 'filterTemplateParameters'));
93
94     // register our shutdown function
95     register_shutdown_function(array($this, 'shutdown'));
96   }
97
98   /**
99    * Retrieves the singleton instance of this class.
100    *
101    * @param  string    $name   The name of the sfContext to retrieve.
102    * @param  string    $class  The context class to use (sfContext by default)
103    *
104    * @return sfContext An sfContext implementation instance.
105    */
106   static public function getInstance($name = null, $class = __CLASS__)
107   {
108     if (is_null($name))
109     {
110       $name = self::$current;
111     }
112
113     if (!isset(self::$instances[$name]))
114     {
115       throw new sfException(sprintf('The "%s" context does not exist.', $name));
116     }
117
118     return self::$instances[$name];
119   }
120
121   /**
122    * Checks to see if there has been a context created
123    *
124    * @param  string $name  The name of the sfContext to check for
125    *
126    * @return bool true is instanced, otherwise false
127    */
128
129   public static function hasInstance($name = null)
130   {
131     if (is_null($name))
132     {
133       $name = self::$current;
134     }
135
136     return isset(self::$instances[$name]);
137   }
138
139   /**
140    * Loads the symfony factories.
141    */
142   public function loadFactories()
143   {
144     if (sfConfig::get('sf_use_database'))
145     {
146       // setup our database connections
147       $this->factories['databaseManager'] = new sfDatabaseManager($this->configuration, array('auto_shutdown' => false));
148     }
149
150     // create a new action stack
151     $this->factories['actionStack'] = new sfActionStack();
152
153     // include the factories configuration
154     require($this->configuration->getConfigCache()->checkConfig('config/factories.yml'));
155
156     $this->dispatcher->notify(new sfEvent($this, 'context.load_factories'));
157   }
158
159   /**
160    * Dispatches the current request.
161    */
162   public function dispatch()
163   {
164     $this->getController()->dispatch();
165   }
166
167   /**
168    * Sets the current context to something else
169    *
170    * @param string $name  The name of the context to switch to
171    *
172    */
173   public static function switchTo($name)
174   {
175     if (!isset(self::$instances[$name]))
176     {
177       $currentConfiguration = sfContext::getInstance()->getConfiguration();
178       sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration($name, $currentConfiguration->getEnvironment(), $currentConfiguration->isDebug()));
179     }
180
181     self::$current = $name;
182
183     sfContext::getInstance()->getConfiguration()->activate();
184   }
185
186   /**
187    * Returns the configuration instance.
188    *
189    * @return sfApplicationConfiguration  The current application configuration instance
190    */
191   public function getConfiguration()
192   {
193     return $this->configuration;
194   }
195
196   /**
197    * Retrieves the current event dispatcher.
198    *
199    * @return sfEventDispatcher An sfEventDispatcher instance
200    */
201   public function getEventDispatcher()
202   {
203     return $this->dispatcher;
204   }
205
206   /**
207    * Retrieve the action name for this context.
208    *
209    * @return string The currently executing action name, if one is set,
210    *                otherwise null.
211    */
212   public function getActionName()
213   {
214     // get the last action stack entry
215     if ($this->factories['actionStack'] && $lastEntry = $this->factories['actionStack']->getLastEntry())
216     {
217       return $lastEntry->getActionName();
218     }
219   }
220
221
222   /**
223    * Retrieve the ActionStack.
224    *
225    * @return sfActionStack the sfActionStack instance
226    */
227   public function getActionStack()
228   {
229     return $this->factories['actionStack'];
230   }
231
232   /**
233    * Retrieve the controller.
234    *
235    * @return sfController The current sfController implementation instance.
236    */
237    public function getController()
238    {
239      return isset($this->factories['controller']) ? $this->factories['controller'] : null;
240    }
241
242    /**
243     * Retrieve the logger.
244     *
245     * @return sfLogger The current sfLogger implementation instance.
246     */
247    public function getLogger()
248    {
249      if (!isset($this->factories['logger']))
250      {
251        $this->factories['logger'] = new sfNoLogger($this->dispatcher);
252      }
253
254      return $this->factories['logger'];
255    }
256
257   /**
258    * Retrieve a database connection from the database manager.
259    *
260    * This is a shortcut to manually getting a connection from an existing
261    * database implementation instance.
262    *
263    * If the [sf_use_database] setting is off, this will return null.
264    *
265    * @param  name  $name  A database name.
266    *
267    * @return mixed A database instance.
268    *
269    * @throws sfDatabaseException if the requested database name does not exist.
270    */
271   public function getDatabaseConnection($name = 'default')
272   {
273     if (!is_null($this->factories['databaseManager']))
274     {
275       return $this->factories['databaseManager']->getDatabase($name)->getConnection();
276     }
277
278     return null;
279   }
280
281   public function retrieveObjects($class, $peerMethod)
282   {
283     $retrievingClass = 'sf'.ucfirst(sfConfig::get('sf_orm', 'propel')).'DataRetriever';
284
285     return call_user_func(array($retrievingClass, 'retrieveObjects'), $class, $peerMethod);
286   }
287
288   /**
289    * Retrieve the database manager.
290    *
291    * @return sfDatabaseManager The current sfDatabaseManager instance.
292    */
293   public function getDatabaseManager()
294   {
295     return isset($this->factories['databaseManager']) ? $this->factories['databaseManager'] : null;
296   }
297
298   /**
299    * Retrieve the module directory for this context.
300    *
301    * @return string An absolute filesystem path to the directory of the
302    *                currently executing module, if one is set, otherwise null.
303    */
304   public function getModuleDirectory()
305   {
306     // get the last action stack entry
307     if (isset($this->factories['actionStack']) && $lastEntry = $this->factories['actionStack']->getLastEntry())
308     {
309       return sfConfig::get('sf_app_module_dir').'/'.$lastEntry->getModuleName();
310     }
311   }
312
313   /**
314    * Retrieve the module name for this context.
315    *
316    * @return string The currently executing module name, if one is set,
317    *                otherwise null.
318    */
319   public function getModuleName()
320   {
321     // get the last action stack entry
322     if (isset($this->factories['actionStack']) && $lastEntry = $this->factories['actionStack']->getLastEntry())
323     {
324       return $lastEntry->getModuleName();
325     }
326   }
327
328   /**
329    * Retrieve the request.
330    *
331    * @return sfRequest The current sfRequest implementation instance.
332    */
333   public function getRequest()
334   {
335     return isset($this->factories['request']) ? $this->factories['request'] : null;
336   }
337
338   /**
339    * Retrieve the response.
340    *
341    * @return sfResponse The current sfResponse implementation instance.
342    */
343   public function getResponse()
344   {
345     return isset($this->factories['response']) ? $this->factories['response'] : null;
346   }
347
348   /**
349    * Set the response object.
350    *
351    * @param sfResponse $response  An sfResponse instance.
352    *
353    * @return void
354    */
355   public function setResponse($response)
356   {
357     $this->factories['response'] = $response;
358   }
359
360   /**
361    * Retrieve the storage.
362    *
363    * @return sfStorage The current sfStorage implementation instance.
364    */
365   public function getStorage()
366   {
367     return isset($this->factories['storage']) ? $this->factories['storage'] : null;
368   }
369
370   /**
371    * Retrieve the view cache manager
372    *
373    * @return sfViewCacheManager The current sfViewCacheManager implementation instance.
374    */
375   public function getViewCacheManager()
376   {
377     return isset($this->factories['viewCacheManager']) ? $this->factories['viewCacheManager'] : null;
378   }
379
380   /**
381    * Retrieve the i18n instance
382    *
383    * @return sfI18N The current sfI18N implementation instance.
384    */
385   public function getI18N()
386   {
387     if (!sfConfig::get('sf_i18n'))
388     {
389       throw new sfConfigurationException('You must enabled i18n support in your settings.yml configuration file.');
390     }
391
392     return $this->factories['i18n'];
393   }
394
395   /**
396    * Retrieve the routing instance.
397    *
398    * @return sfRouting The current sfRouting implementation instance.
399    */
400   public function getRouting()
401   {
402     return isset($this->factories['routing']) ? $this->factories['routing'] : null;
403   }
404
405   /**
406    * Retrieve the user.
407    *
408    * @return sfUser The current sfUser implementation instance.
409    */
410   public function getUser()
411   {
412     return isset($this->factories['user']) ? $this->factories['user'] : null;
413   }
414
415   /**
416    * Returns the configuration cache.
417    *
418    * @return sfConfigCache A sfConfigCache instance
419    */
420   public function getConfigCache()
421   {
422     return $this->configuration->getConfigCache();
423   }
424
425   /**
426    * Gets an object from the current context.
427    *
428    * @param  string $name  The name of the object to retrieve
429    *
430    * @return object The object associated with the given name
431    */
432   public function get($name)
433   {
434     if (!$this->has($name))
435     {
436       throw new sfException(sprintf('The "%s" object does not exist in the current context.', $name));
437     }
438
439     return $this->factories[$name];
440   }
441
442   /**
443    * Puts an object in the current context.
444    *
445    * @param string $name    The name of the object to store
446    * @param object $object  The object to store
447    */
448   public function set($name, $object)
449   {
450     $this->factories[$name] = $object;
451   }
452
453   /**
454    * Returns true if an object is currently stored in the current context with the given name, false otherwise.
455    *
456    * @param  string $name  The object name
457    *
458    * @return bool true if the object is not null, false otherwise
459    */
460   public function has($name)
461   {
462     return isset($this->factories[$name]);
463   }
464
465   /**
466    * Listens to the template.filter_parameters event.
467    *
468    * @param  sfEvent $event       An sfEvent instance
469    * @param  array   $parameters  An array of template parameters to filter
470    *
471    * @return array   The filtered parameters array
472    */
473   public function filterTemplateParameters(sfEvent $event, $parameters)
474   {
475     $parameters['sf_context']  = $this;
476     $parameters['sf_request']  = $this->factories['request'];
477     $parameters['sf_params']   = $this->factories['request']->getParameterHolder();
478     $parameters['sf_response'] = $this->factories['response'];
479     $parameters['sf_user']     = $this->factories['user'];
480
481     return $parameters;
482   }
483
484   /**
485    * Execute the shutdown procedure.
486    *
487    * @return void
488    */
489   public function shutdown()
490   {
491     // shutdown all factories
492     if($this->has('user'))
493     {
494       $this->getUser()->shutdown();
495       $this->getStorage()->shutdown();
496     }
497
498     if ($this->has('routing'))
499     {
500         $this->getRouting()->shutdown();
501     }
502
503     if (sfConfig::get('sf_use_database'))
504     {
505       $this->getDatabaseManager()->shutdown();
506     }
507
508     if (sfConfig::get('sf_logging_enabled'))
509     {
510       $this->getLogger()->shutdown();
511     }
512   }
513 }
514
Note: See TracBrowser for help on using the browser.