Development

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

You must first sign up to be able to contribute.

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

Revision 16165, 7.7 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  * current controller, request, and user implementation instances are also
16  * provided.
17  *
18  * @package    symfony
19  * @subpackage util
20  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
21  * @author     Sean Kerr <sean@code-box.org>
22  * @version    SVN: $Id$
23  */
24 class sfContext
25 {
26   protected
27     $actionStack       = null,
28     $controller        = null,
29     $databaseManager   = null,
30     $request           = null,
31     $response          = null,
32     $storage           = null,
33     $viewCacheManager  = null,
34     $i18n              = null,
35     $logger            = null,
36     $user              = null;
37
38   protected static
39     $instance          = null;
40
41   /**
42    * Removes current sfContext instance
43    *
44    * This method only exists for testing purpose. Don't use it in your application code.
45    */
46   public static function removeInstance()
47   {
48     self::$instance = null;
49   }
50
51   protected function initialize()
52   {
53     $this->logger = sfLogger::getInstance();
54     if (sfConfig::get('sf_logging_enabled'))
55     {
56       $this->logger->info('{sfContext} initialization');
57     }
58
59     if (sfConfig::get('sf_use_database'))
60     {
61       // setup our database connections
62       $this->databaseManager = new sfDatabaseManager();
63       $this->databaseManager->initialize();
64     }
65
66     // create a new action stack
67     $this->actionStack = new sfActionStack();
68
69     // include the factories configuration
70     require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_config_dir_name').'/factories.yml'));
71
72     // register our shutdown function
73     register_shutdown_function(array($this, 'shutdown'));
74   }
75
76   /**
77    * Retrieve the singleton instance of this class.
78    *
79    * @return sfContext A sfContext implementation instance.
80    */
81   public static function getInstance()
82   {
83     if (!isset(self::$instance))
84     {
85       $class = __CLASS__;
86       self::$instance = new $class();
87       self::$instance->initialize();
88     }
89
90     return self::$instance;
91   }
92
93   public static function hasInstance()
94   {
95     return isset(self::$instance);
96   }
97
98   /**
99    * Retrieve the action name for this context.
100    *
101    * @return string The currently executing action name, if one is set,
102    *                otherwise null.
103    */
104   public function getActionName()
105   {
106     // get the last action stack entry
107     if ($this->actionStack && $lastEntry = $this->actionStack->getLastEntry())
108     {
109       return $lastEntry->getActionName();
110     }
111   }
112
113
114   /**
115    * Retrieve the ActionStack.
116    *
117    * @return sfActionStack the sfActionStack instance
118    */
119   public function getActionStack()
120   {
121     return $this->actionStack;
122   }
123
124   /**
125    * Retrieve the controller.
126    *
127    * @return sfController The current sfController implementation instance.
128    */
129    public function getController()
130    {
131      return $this->controller;
132    }
133
134    /**
135     * Retrieve the logger.
136     *
137     * @return sfLogger The current sfLogger implementation instance.
138     */
139    public function getLogger()
140    {
141      return $this->logger;
142    }
143
144   /**
145    * Retrieve a database connection from the database manager.
146    *
147    * This is a shortcut to manually getting a connection from an existing
148    * database implementation instance.
149    *
150    * If the [sf_use_database] setting is off, this will return null.
151    *
152    * @param name A database name.
153    *
154    * @return mixed A Database instance.
155    *
156    * @throws <b>sfDatabaseException</b> If the requested database name does not exist.
157    */
158   public function getDatabaseConnection($name = 'default')
159   {
160     if ($this->databaseManager != null)
161     {
162       return $this->databaseManager->getDatabase($name)->getConnection();
163     }
164
165     return null;
166   }
167
168   public function retrieveObjects($class, $peerMethod)
169   {
170     $retrievingClass = 'sf'.ucfirst(sfConfig::get('sf_orm', 'propel')).'DataRetriever';
171
172     return call_user_func(array($retrievingClass, 'retrieveObjects'), $class, $peerMethod);
173   }
174
175   /**
176    * Retrieve the database manager.
177    *
178    * @return sfDatabaseManager The current sfDatabaseManager instance.
179    */
180   public function getDatabaseManager()
181   {
182     return $this->databaseManager;
183   }
184
185   /**
186    * Retrieve the module directory for this context.
187    *
188    * @return string An absolute filesystem path to the directory of the
189    *                currently executing module, if one is set, otherwise null.
190    */
191   public function getModuleDirectory()
192   {
193     // get the last action stack entry
194     if ($this->actionStack && $lastEntry = $this->actionStack->getLastEntry())
195     {
196       return sfConfig::get('sf_app_module_dir').'/'.$lastEntry->getModuleName();
197     }
198   }
199
200   /**
201    * Retrieve the module name for this context.
202    *
203    * @return string The currently executing module name, if one is set,
204    *                otherwise null.
205    */
206   public function getModuleName()
207   {
208     // get the last action stack entry
209     if ($this->actionStack && $lastEntry = $this->actionStack->getLastEntry())
210     {
211       return $lastEntry->getModuleName();
212     }
213   }
214
215   /**
216    * Retrieve the curretn view instance for this context.
217    *
218    * @return sfView The currently view instance, if one is set,
219    *                otherwise null.
220    */
221   public function getCurrentViewInstance()
222   {
223     // get the last action stack entry
224     if ($this->actionStack && $lastEntry = $this->actionStack->getLastEntry())
225     {
226       return $lastEntry->getViewInstance();
227     }
228   }
229
230   /**
231    * Retrieve the request.
232    *
233    * @return sfRequest The current sfRequest implementation instance.
234    */
235   public function getRequest()
236   {
237     return $this->request;
238   }
239
240   /**
241    * Retrieve the response.
242    *
243    * @return sfResponse The current sfResponse implementation instance.
244    */
245   public function getResponse()
246   {
247     return $this->response;
248   }
249
250   /**
251    * Set the response object.
252    *
253    * @param sfResponse A sfResponse instance.
254    *
255    * @return void.
256    */
257   public function setResponse($response)
258   {
259     $this->response = $response;
260   }
261
262   /**
263    * Retrieve the storage.
264    *
265    * @return sfStorage The current sfStorage implementation instance.
266    */
267   public function getStorage()
268   {
269     return $this->storage;
270   }
271
272   /**
273    * Retrieve the view cache manager
274    *
275    * @return sfViewCacheManager The current sfViewCacheManager implementation instance.
276    */
277   public function getViewCacheManager()
278   {
279     return $this->viewCacheManager;
280   }
281
282   /**
283    * Retrieve the i18n instance
284    *
285    * @return sfI18N The current sfI18N implementation instance.
286    */
287   public function getI18N()
288   {
289     if (!$this->i18n && sfConfig::get('sf_i18n'))
290     {
291       $this->i18n = sfI18N::getInstance();
292       $this->i18n->initialize($this);
293     }
294
295     return $this->i18n;
296   }
297
298   /**
299    * Retrieve the user.
300    *
301    * @return sfUser The current sfUser implementation instance.
302    */
303   public function getUser()
304   {
305     return $this->user;
306   }
307
308   /**
309    * Execute the shutdown procedure.
310    *
311    * @return void
312    */
313   public function shutdown()
314   {
315     // shutdown all factories
316     $this->getUser()->shutdown();
317     $this->getStorage()->shutdown();
318     $this->getRequest()->shutdown();
319     $this->getResponse()->shutdown();
320
321     if (sfConfig::get('sf_logging_enabled'))
322     {
323       $this->getLogger()->shutdown();
324     }
325
326     if (sfConfig::get('sf_use_database'))
327     {
328       $this->getDatabaseManager()->shutdown();
329     }
330
331     if (sfConfig::get('sf_cache'))
332     {
333       $this->getViewCacheManager()->shutdown();
334     }
335   }
336 }
337
Note: See TracBrowser for help on using the browser.