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. |
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. |
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. |
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. |
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)
67 $pref = strtolower(get_class($this));
69 $file = "/{$pref}/{$file}.php";
71 $file = HAANGA_DIR
.$file;
76 final public function getFunctionAlias($name)
78 if (!$this->isValid($name)) {
81 $zclass = $this->getClassName($name);
82 $properties = get_class_vars($zclass);
83 if (isset($properties['php_alias'])) {
84 return $properties['php_alias'];
89 // generator(string $name, Haanga_Compiler $compiler, Array $args) {{{
91 * Executer the generator method of the extension. If
92 * the extension doesn't has any generator method, an empty
95 * @param string $name extension name
96 * @param Haanga_Main Compiler object
101 function generator($name, Haanga_Main
$compiler, $args)
103 if (!$this->hasGenerator($name)) {
106 $zclass = $this->getClassName($name);
107 return $zclass::generator($compiler, $args);
111 // hasGenerator(string $name) {{{
113 * Return TRUE if the extension has a
116 * @param string $name Extension name
120 function hasGenerator($name)
122 if (!$this->isValid($name)) {
125 $zclass = $this->getClassName($name);
126 return is_callable(array($zclass, 'generator'));
130 // getFunctionBody(string $name, string $name) {{{
132 * Return the body function of the custom extension main method.
134 * @param string $name
135 * @param string $name
139 static function getFunctionBody($name, $name)
141 if (!$this->isValid($name)) {
144 $zclass = $this->getClassName($name);
145 if (!is_callable(array($zclass, 'main'))) {
146 throw new CompilerException("{$name}: missing main method in {$zclass} class");
149 $reflection = new ReflectionMethod($zclass, 'main');
150 $content = file($this->getFilePath($name));
152 $start = $reflection->getStartLine()-1;
153 $end = $reflection->getEndLine();
154 $content = array_slice($content, $start, $end-$start);
156 $content[0] = str_replace("main", $name, $content[0]);
158 return implode("", $content);
169 * vim600: sw=4 ts=4 fdm=marker