Development

/branches/1.2/lib/plugins/sfPropelPlugin/lib/addon/sfPropel.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfPropelPlugin/lib/addon/sfPropel.class.php

Revision 14025, 3.7 kB (checked in by Kris.Wallsmith, 6 years ago)

[1.2] added clearing of Propel instance pools between functional test requests (closes #5295)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Initialization for propel and i18n propel integration.
13  *
14  * @package    symfony
15  * @subpackage propel
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfPropel
20 {
21   static protected
22     $initialized    = false,
23     $defaultCulture = 'en';
24
25   /**
26    * Initialize sfymfony propel
27    *
28    * @param sfEventDispatcher $dispatcher
29    * @param string $culture
30    */
31   static public function initialize(sfEventDispatcher $dispatcher, $culture = null)
32   {
33     if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
34     {
35       // add propel logger
36       Propel::setLogger(new sfPropelLogger($dispatcher));
37     }
38
39     // propel initialization
40     $configuration = sfPropelDatabase::getConfiguration();
41     if($configuration)
42     {
43       Propel::setConfiguration($configuration);
44
45       if(!Propel::isInit())
46       {
47         Propel::initialize();
48       }
49     }
50
51     $dispatcher->connect('user.change_culture', array('sfPropel', 'listenToChangeCultureEvent'));
52
53     if (!is_null($culture))
54     {
55       self::setDefaultCulture($culture);
56     }
57     else if (class_exists('sfContext', false) && sfContext::hasInstance() && $user = sfContext::getInstance()->getUser())
58     {
59       self::setDefaultCulture($user->getCulture());
60     }
61
62     self::$initialized = true;
63   }
64
65   /**
66    * Sets the default culture
67    *
68    * @param string $culture
69    */
70   static public function setDefaultCulture($culture)
71   {
72     self::$defaultCulture = $culture;
73   }
74
75   /**
76    * Return the default culture
77    *
78    * @return string the default culture
79    */
80   static public function getDefaultCulture()
81   {
82     if (!self::$initialized && class_exists('sfProjectConfiguration', false))
83     {
84       self::initialize(sfProjectConfiguration::getActive()->getEventDispatcher());
85     }
86
87     return self::$defaultCulture;
88   }
89
90   /**
91    * Listens to the user.change_culture event.
92    *
93    * @param sfEvent An sfEvent instance
94    *
95    */
96   static public function listenToChangeCultureEvent(sfEvent $event)
97   {
98     self::setDefaultCulture($event['culture']);
99   }
100
101   /**
102    * Include once a file specified in DOT notation and return unqualified classname.
103    *
104    * This method is the same as in Propel::import().
105    * The only difference is that this one takes the autoloading into account.
106    *
107    * @see Propel::import()
108    */
109   public static function import($path)
110   {
111     // extract classname
112     if (($pos = strrpos($path, '.')) === false)
113     {
114       $class = $path;
115     }
116     else
117     {
118       $class = substr($path, $pos + 1);
119     }
120
121     // check if class exists
122     if (class_exists($class, true))
123     {
124       return $class;
125     }
126
127     // turn to filesystem path
128     $path = strtr($path, '.', DIRECTORY_SEPARATOR).'.php';
129
130     // include class
131     $ret = include_once($path);
132     if ($ret === false)
133     {
134       throw new PropelException("Unable to import class: ".$class." from ".$path);
135     }
136
137     // return qualified name
138     return $class;
139   }
140
141   /**
142    * Clears all instance pools.
143    */
144   static public function clearAllInstancePools()
145   {
146     $files = sfFinder::type('file')->name('*MapBuilder.php')->in(sfProjectConfiguration::getActive()->getModelDirs());
147     foreach ($files as $file)
148     {
149       $omClass = basename($file, 'MapBuilder.php');
150       if (class_exists($omClass) && is_subclass_of($omClass, 'BaseObject'))
151       {
152         $peer = constant($omClass.'::PEER');
153         call_user_func(array($peer, 'clearInstancePool'));
154       }
155     }
156   }
157 }
158
Note: See TracBrowser for help on using the browser.