root/trunk/XJConf/XJConfFacade.php

Revision 138, 6.0 kB (checked in by mikey, 6 months ago)

ported feature addition from PRE_NAMESPACES branch

Line 
1 <?php
2 /**
3  * Facade for XJConf.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     XJConf
7  */
8 namespace net::xjconf;
9 use net::xjconf::definitions::NamespaceDefinitions;
10 use net::xjconf::ext::Extension;
11 /**
12  * Facade for XJConf.
13  *
14  * @package     XJConf
15  */
16 class XJConfFacade
17 {
18     /**
19      * list of class loaders to use
20      *
21      * @var  array<string,XJConfClassLoader>
22      */
23     protected $classLoaders         = array();
24     /**
25      * list of extensions to use
26      *
27      * @var  array<string,Extension>
28      */
29     protected $extensions           = array();
30     /**
31      * list of namespace definitions to merge with default one
32      *
33      * @var  array<NamespaceDefinition>
34      */
35     protected $namespaceDefinitions = array();
36     /**
37      * the definition parser
38      *
39      * @var  DefinitionParser
40      */
41     protected $definitionParser;
42     /**
43      * the xml parser
44      *
45      * @var  XmlParser
46      */
47     protected $xmlParser;
48     
49     /**
50      * construct the facade
51      *
52      * @param  array  $classLoaders<string,XJConfClassLoader>  optional  list of class loaders for given namespaces
53      */
54     public function __construct(array $classLoaders = array())
55     {
56         $this->classLoaders = $classLoaders;
57     }
58
59     /**
60      * add an extension that handles all tags in given namespace
61      *
62      * @param  Extension  $ext        use this extension to handle all tags in given namespace
63      * @param  string     $namespace  optional  handle all tags in this namespace with given extension
64      */
65     public function addExtension(Extension $ext, $namespace = null)
66     {
67         if (null == $namespace) {
68             $namespace = $ext->getNamespace();
69         }
70         
71         $this->extensions[$namespace] = $ext;
72     }
73     
74     /**
75      * enables xinclude
76      */
77     public function enableXIncludes()
78     {
79         $xincludeExtension = new net::xjeconf::ext::xinc::XInclude();
80         $this->addExtension($xincludeExtension);
81     }
82     
83     /**
84      * add a namespace definition
85      *
86      * @param  NamespaceDefinition  $namespaceDefinition
87      */
88     public function addNamespaceDefinition(NamespaceDefinition $namespaceDefinition)
89     {
90         $this->namespaceDefinitions[$namespaceDefinition->getNamespaceURI()] = $namespaceDefinition;
91     }
92     
93     /**
94      * add a namespace definition
95      *
96      * @param  NamespaceDefinitions  $namespaceDefinitions
97      */
98     public function addNamespaceDefinitions(NamespaceDefinitions $namespaceDefinitions)
99     {
100         $this->namespaceDefinitions = array_merge($this->namespaceDefinitions, $namespaceDefinitions->getDefinedNamespaces());
101     }
102     
103     /**
104      * parses a definition and returns the namespace definitions
105      *
106      * @param   string                $definitionFile
107      * @return  NamespaceDefinitions
108      */
109     public function parseDefinition($definitionFile)
110     {
111         if (null === $this->definitionParser) {
112             $this->definitionParser = new DefinitionParser($this->classLoaders);
113         }
114         
115         return $this->definitionParser->parse($definitionFile);
116     }
117     
118     /**
119      * parses a definition file and adds its definitions
120      *
121      * @param  string  $definitionFile
122      */
123     public function addDefinition($definitionFile)
124     {
125         $this->addNamespaceDefinitions($this->parseDefinition($definitionFile));
126     }
127     
128     /**
129      * parses a definition file and adds its definitions
130      *
131      * @param  array  $definitions
132      */
133     public function addDefinitions(array $definitions)
134     {
135         foreach ($definitions as $definition) {
136             $this->addNamespaceDefinitions($this->parseDefinition($definition));
137         }
138     }
139     
140     /**
141      * parses a given file and creates the data structure described in this file
142      *
143      * @param   string               $filename
144      * @throws  stubXJConfException
145      */
146     public function parse($filename)
147     {
148         if (null === $this->xmlParser) {
149             $this->xmlParser = new XmlParser();
150         }
151         
152         $namespaceDefinitions = new NamespaceDefinitions();
153         foreach ($this->namespaceDefinitions as $namespaceURI => $namespaceDefintion) {
154             $namespaceDefinitions->addNamespaceDefinition($namespaceURI, $namespaceDefintion);
155         }
156         
157         $this->xmlParser->setTagDefinitions($namespaceDefinitions);
158         foreach ($this->extensions as $namespace => $extension) {
159             $this->xmlParser->addExtension($extension, $namespace);
160         }
161         
162         $this->xmlParser->parse($filename);
163     }
164     
165     /**
166      * checks whether a data structure associated with this name exists
167      *
168      * @param   string               $name
169      * @return  bool
170      * @throws  XJConfException
171      */
172     public function hasConfigValue($name)
173     {
174         if (null === $this->xmlParser) {
175             throw new XJConfException('Invalid state: needs to parse first.');
176         }
177         
178         return $this->xmlParser->hasConfigValue($name);
179     }
180     
181     /**
182      * returns the data structure associated with this name
183      *
184      * @param   string               $name
185      * @return  mixed
186      * @throws  XJConfException
187      */
188     public function getConfigValue($name)
189     {
190         if (null === $this->xmlParser) {
191             throw new XJConfException('Invalid state: needs to parse first.');
192         }
193         
194         return $this->xmlParser->getConfigValue($name);
195     }
196     
197     /**
198      * returns a list of all data structures
199      *
200      * @return  mixed
201      * @throws  XJConfException
202      */
203     public function getConfigValues()
204     {
205         if (null === $this->xmlParser) {
206             throw new XJConfException('Invalid state: needs to parse first.');
207         }
208         
209         return $this->xmlParser->getConfigValues();
210     }
211
212     /**
213      * clears parsed config values
214      */
215     public function clearConfigValues()
216     {
217         if (null == $this->xmlParser) {
218             throw new XJConfException('Invalid state: needs to parse first.');
219         }
220         
221         $this->xmlParser->clearConfigValues();
222     }
223 }
224 ?>
Note: See TracBrowser for help on using the browser.