root/trunk/src/main/php/net/stubbles/lang/initializer/stubGeneralInitializer.php

Revision 1538, 6.6 kB (checked in by mikey, 3 months ago)

refactoring #139, part 4: moved net::stubbles::util::stubGeneralInitializer to net::stubbles::lang::initializer::stubGeneralInitializer, net::stubbles::util::stubRegistryInitializer to net::stubbles::lang::initializer::stubRegistryInitializer and net::stubbles::util::stubRegistryXJConfInitializer to net::stubbles::lang::initializer::stubRegistryXJConfInitializer

Line 
1 <?php
2 /**
3  * Initializer for general purpose stuff.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  lang_initializer
8  */
9 stubClassLoader::load('net::stubbles::lang::exceptions::stubRuntimeException',
10                       'net::stubbles::lang::initializer::stubInitializer'
11 );
12 /**
13  * Initializer for general purpose stuff.
14  *
15  * @author      Frank Kleine <mikey@stubbles.net>
16  * @package     stubbles
17  * @subpackage  lang_initializer
18  */
19 class stubGeneralInitializer extends stubBaseObject implements stubInitializer
20 {
21     /**
22      * list of en-/disabled features
23      *
24      * @var  array<string,bool>
25      */
26     protected $features = array('logger' => true,
27                                 'rdbms'  => false,
28                                 'cache'  => false,
29                                 'events' => false
30                           );
31     /**
32      * list of initializer classes for the single features
33      *
34      * @var  array<string,string>
35      */
36     protected $classes  = array('logger' => 'net::stubbles::util::log::stubLoggerXJConfInitializer',
37                                 'rdbms'  => 'net::stubbles::rdbms::stubDatabaseXJConfInitializer',
38                                 'cache'  => 'net::stubbles::util::cache::stubCacheXJConfInitializer',
39                                 'events' => 'net::stubbles::events::stubEventsXJConfInitializer'
40                           );
41
42     /**
43      * constructor
44      *
45      * @param  array  $features  optional  list of en-/disabled features
46      * @param  array  $classes   optional  list of initializer classes for the single features
47      */
48     public function __construct(array $features = null, array $classes = null)
49     {
50         if (null !== $features) {
51             $this->features = array_merge($this->features, $features);
52         }
53         
54         if (null !== $classes) {
55             $this->classes = array_merge($this->classes, $classes);
56         }
57     }
58
59     /**
60      * initializing method
61      */
62     public function init()
63     {
64         if ($this->isLoggingEnabled() === true) {
65             $this->getLoggerInitializer()->init();
66         }
67         
68         if ($this->isCachingEnabled() === true) {
69             $this->getCacheInitializer()->init();
70         }
71         
72         if ($this->isDatabaseEnabled() === true) {
73             $this->getDatabaseInitializer()->init();
74         }
75         
76         if ($this->isEventHandlingEnabled() === true) {
77             $this->getEventsInitializer()->init();
78         }
79     }
80
81     /**
82      * checks whether logging is enabled or not
83      *
84      * @return  bool
85      */
86     public function isLoggingEnabled()
87     {
88         return $this->features['logger'];
89     }
90
91     /**
92      * returns the logger initializer to be used
93      *
94      * @return  stubLoggerInitializer
95      * @throws  stubRuntimeException
96      */
97     public function getLoggerInitializer()
98     {
99         if (true === $this->features['logger']) {
100             stubClassLoader::load('net::stubbles::util::log::stubLoggerInitializer');
101             $loggerInitializer = $this->getInitializer('logger');
102             if (($loggerInitializer instanceof stubLoggerInitializer) === false) {
103                 throw new stubRuntimeException('Configured logger initializer is not an instance of net::stubbles::util::log::stubLoggerInitializer');
104             }
105             
106             return $loggerInitializer;
107         }
108         
109         return null;
110     }
111
112     /**
113      * checks whether database connections are enabled or not
114      *
115      * @return  bool
116      */
117     public function isDatabaseEnabled()
118     {
119         return $this->features['rdbms'];
120     }
121
122     /**
123      * returns the database initializer to be used
124      *
125      * @return  stubDatabaseInitializer
126      * @throws  stubRuntimeException
127      */
128     public function getDatabaseInitializer()
129     {
130         if (true === $this->features['rdbms']) {
131             stubClassLoader::load('net::stubbles::rdbms::stubDatabaseInitializer');
132             $databaseInitializer = $this->getInitializer('rdbms');
133             if (($databaseInitializer instanceof stubDatabaseInitializer) === false) {
134                 throw new stubRuntimeException('Configured database initializer is not an instance of net::stubbles::rdbms::stubDatabaseInitializer');
135             }
136             
137             return $databaseInitializer;
138         }
139         
140         return null;
141     }
142
143     /**
144      * checks whether caching is enabled or not
145      *
146      * @return  bool
147      */
148     public function isCachingEnabled()
149     {
150         return $this->features['cache'];
151     }
152
153     /**
154      * returns the cache initializer to be used
155      *
156      * @return  stubCacheInitializer
157      * @throws  stubRuntimeException
158      */
159     public function getCacheInitializer()
160     {
161         if (true === $this->features['cache']) {
162             stubClassLoader::load('net::stubbles::util::cache::stubCacheInitializer');
163             $cacheInitializer = $this->getInitializer('cache');
164             if (($cacheInitializer instanceof stubCacheInitializer) === false) {
165                 throw new stubRuntimeException('Configured cache initializer is not an instance of net::stubbles::util::cache::stubCacheInitializer');
166             }
167             
168             return $cacheInitializer;
169         }
170         
171         return null;
172     }
173
174     /**
175      * checks whether caching is enabled or not
176      *
177      * @return  bool
178      */
179     public function isEventHandlingEnabled()
180     {
181         return $this->features['events'];
182     }
183
184     /**
185      * returns the events initializer to be used
186      *
187      * @return  stubEventsInitializer
188      * @throws  stubRuntimeException
189      */
190     public function getEventsInitializer()
191     {
192         if (true === $this->features['events']) {
193             stubClassLoader::load('net::stubbles::events::stubEventsInitializer');
194             $eventsInitializer = $this->getInitializer('events');
195             if (($eventsInitializer instanceof stubEventsInitializer) === false) {
196                 throw new stubRuntimeException('Configured events initializer is not an instance of net::stubbles::events::stubEventsInitializer');
197             }
198             
199             return $eventsInitializer;
200         }
201         
202         return null;
203     }
204
205     /**
206      * helper method to create the initializer
207      *
208      * @param   string           $type
209      * @return  stubInitializer
210      */
211     protected function getInitializer($type)
212     {
213         $nqClassName = stubClassLoader::getNonQualifiedClassName($this->classes[$type]);
214         if (class_exists($nqClassName) === false) {
215             stubClassLoader::load($this->classes[$type]);
216         }
217         
218         return new $nqClassName();
219     }
220 }
221 ?>
Note: See TracBrowser for help on using the browser.