- Added tests, tags and filters
[haanga.git] / haanga / extensions.php
blob21777064f1bb6069c11cbcac32cd0a9dfe33bc97
1 <?php
2 /*
3 +---------------------------------------------------------------------------------+
4 | Copyright (c) 2010 Haanga |
5 +---------------------------------------------------------------------------------+
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: |
8 | 1. Redistributions of source code must retain the above copyright |
9 | notice, this list of conditions and the following disclaimer. |
10 | |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | 3. All advertising materials mentioning features or use of this software |
16 | must display the following acknowledgement: |
17 | This product includes software developed by César D. Rodas. |
18 | |
19 | 4. Neither the name of the César D. Rodas nor the |
20 | names of its contributors may be used to endorse or promote products |
21 | derived from this software without specific prior written permission. |
22 | |
23 | THIS SOFTWARE IS PROVIDED BY CÉSAR D. RODAS ''AS IS'' AND ANY |
24 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
26 | DISCLAIMED. IN NO EVENT SHALL CÉSAR D. RODAS BE LIABLE FOR ANY |
27 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
28 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
29 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
30 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE |
33 +---------------------------------------------------------------------------------+
34 | Authors: César Rodas <crodas@php.net> |
35 +---------------------------------------------------------------------------------+
38 Abstract Class Extensions
40 private static $_instances;
42 final private function __construct()
46 final static function getInstance($name)
48 if (!class_exists($name)) {
49 throw new CompilerExeception("{$name} is not a class");
51 if (!is_subclass_of($name, __CLASS__)) {
52 throw new CompilerExeception("{$name} is not a sub-class of ".__CLASS__);
55 if (!isset(self::$_instances[$name])) {
56 self::$_instances[$name] = new $name;
58 return self::$_instances[$name];
61 abstract function isValid($name);
62 abstract function getClassName($name);
64 function getFilePath($file, $rel=TRUE, $pref=NULL)
66 if (!$pref) {
67 $pref = strtolower(get_class($this));
69 $file = "/{$pref}/{$file}.php";
70 if ($rel) {
71 $file = HAANGA_DIR.$file;
73 return $file;
76 final public function getFunctionAlias($name)
78 if (!$this->isValid($name)) {
79 return NULL;
81 $zclass = $this->getClassName($name);
82 $properties = get_class_vars($zclass);
83 if (isset($properties['php_alias'])) {
84 return $properties['php_alias'];
86 return NULL;
89 // generator(string $name, Haanga_Compiler $compiler, Array $args) {{{
90 /**
91 * Executer the generator method of the extension. If
92 * the extension doesn't has any generator method, an empty
93 * will be returned.
95 * @param string $name extension name
96 * @param Haanga_Main Compiler object
97 * @param array Arrays
98 * @param mixed Extra param
100 * @return array
102 function generator($name, Haanga_Main $compiler, $args, $extra=NULL)
104 if (!$this->hasGenerator($name)) {
105 return array();
107 $zclass = $this->getClassName($name);
108 return $zclass::generator($compiler, $args, $extra);
110 // }}}
112 // hasGenerator(string $name) {{{
113 /**
114 * Return TRUE if the extension has a
115 * generator method
117 * @param string $name Extension name
119 * @return bool
121 function hasGenerator($name)
123 if (!$this->isValid($name)) {
124 return NULL;
126 $zclass = $this->getClassName($name);
127 return is_callable(array($zclass, 'generator'));
129 // }}}
131 // getFunctionBody(string $name, string $name) {{{
133 * Return the body function of the custom extension main method.
135 * @param string $name
136 * @param string $name
138 * @return string
140 static function getFunctionBody($name, $name)
142 if (!$this->isValid($name)) {
143 return NULL;
145 $zclass = $this->getClassName($name);
146 if (!is_callable(array($zclass, 'main'))) {
147 throw new CompilerException("{$name}: missing main method in {$zclass} class");
150 $reflection = new ReflectionMethod($zclass, 'main');
151 $content = file($this->getFilePath($name));
153 $start = $reflection->getStartLine()-1;
154 $end = $reflection->getEndLine();
155 $content = array_slice($content, $start, $end-$start);
157 $content[0] = str_replace("main", $name, $content[0]);
159 return implode("", $content);
161 // }}}
166 * Local variables:
167 * tab-width: 4
168 * c-basic-offset: 4
169 * End:
170 * vim600: sw=4 ts=4 fdm=marker
171 * vim<600: sw=4 ts=4