root/trunk/src/main/php/net/stubbles/websites/stubPageXJConfFactory.php

Revision 1535, 4.5 kB (checked in by mikey, 1 month ago)

refactoring #139, part 2: moved net::stubbles::util::stubFactory to net::stubbles::lang::stubFactory

Line 
1 <?php
2 /**
3  * Class to read the page configuration and to create the page.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  websites
8  */
9 stubClassLoader::load('net::stubbles::lang::stubRegistry',
10                       'net::stubbles::lang::exceptions::stubConfigurationException',
11                       'net::stubbles::util::xjconf::xjconf',
12                       'net::stubbles::websites::stubAbstractPageFactory'
13 );
14 /**
15  * Class to read the page configuration and to create the page.
16  *
17  * @package     stubbles
18  * @subpackage  websites
19  */
20 class stubPageXJConfFactory extends stubAbstractPageFactory
21 {
22     /**
23      * the xml parser
24      *
25      * @var  stubXJConfFacade
26      */
27     private static $xjconf;
28     /**
29      * path to cache files
30      *
31      * @var  string
32      */
33     protected $cachePath;
34     /**
35      * path to config files
36      *
37      * @var  string
38      */
39     protected $configPath;
40
41     /**
42      * constructor
43      *
44      * @param  string  $cachePath   optional  path to cache files
45      * @param  string  $configPath  optional  path to config files
46      */
47     public function __construct($cachePath = null, $configPath = null)
48     {
49         $this->cachePath  = ((null == $cachePath) ? (stubConfig::getCachePath() . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR) : ($cachePath));
50         $this->configPath = ((null == $configPath) ? (stubConfig::getPagePath() . DIRECTORY_SEPARATOR) : ($configPath));
51     }
52
53     /**
54      * checks whether the page factory knows the page or not
55      *
56      * @param   string  $configSource  source of the page configuration to use
57      * @return  bool
58      */
59     public function hasPage($configSource)
60     {
61         return file_exists($this->configPath . $this->pagePrefix . $configSource . '.xml');
62     }
63
64     /**
65      * returns the configured stubPage instance
66      *
67      * @param   string    $configSource  name of the page to retrieve
68      * @return  stubPage
69      */
70     protected function doGetPage($configSource)
71     {
72         $configSource = str_replace('/', DIRECTORY_SEPARATOR, $this->pagePrefix . $configSource);
73         $cacheSource  = $this->cachePath . $configSource . '.cache';
74         $configSource = $this->configPath . $configSource . '.xml';
75         if (file_exists($cacheSource) && filemtime($cacheSource) >= filemtime($configSource)) {
76             $cachedPage = unserialize(file_get_contents($cacheSource));
77             foreach ($cachedPage['classes'] as $class) {
78                 stubClassLoader::load($class);
79             }
80             
81             $page = unserialize($cachedPage['data']);
82             return $page;
83         }
84         
85         $page = $this->getPageFromXJConf($configSource);
86         $cachedPage = array('classes' => array($page->getClassName()),
87                             'data'    => serialize($page)
88                       );
89         foreach ($page->getElements() as $pageElement) {
90             $cachedPage['classes'][] = $pageElement->getClassName();
91             foreach ($pageElement->getRequiredClassNames() as $requiredClassName) {
92                 $cachedPage['classes'][] = $requiredClassName;
93             }
94         }
95         
96         if (file_exists(dirname($cacheSource)) == false) {
97             mkdir(dirname($cacheSource), stubRegistry::getConfig('net.stubbles.filemode', 0700), true);
98         }
99         
100         file_put_contents($cacheSource, serialize($cachedPage));
101         return $page;
102     }
103
104     /**
105      * returns the configured stubPage instance
106      *
107      * @param   string    $configSource   source of the page configuration to use
108      * @return  stubPage
109      * @throws  stubConfigurationException
110      */
111     protected function getPageFromXJConf($configSource)
112     {
113         if (null == self::$xjconf) {
114             stubClassLoader::load('net::stubbles::lang::stubFactory',
115                                   'net::stubbles::util::xjconf::xjconfReal'
116             );
117             self::$xjconf = new stubXJConfFacade(new XJConfFacade(array('__default' => stubXJConfLoader::getInstance())));
118             self::$xjconf->addDefinitions(stubFactory::getResourceURIs('xjconf/websites.xml'));
119             self::$xjconf->addExtension(new stubConfigXJConfExtension());
120         }
121         
122         try {
123             self::$xjconf->parse($configSource);
124             return self::$xjconf->getConfigValue('page');
125         } catch (stubXJConfException $xjce) {
126             throw new stubConfigurationException('Can not read page configuration from ' . $configSource, $xjce);
127         }
128     }
129 }
130 ?>
Note: See TracBrowser for help on using the browser.