root/branches/PRE_NAMESPACES/XJConf/XJConfFacade.php

Revision 137, 6.0 kB (checked in by mikey, 8 months ago)

added XmlParser::clearConfigValues()

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