PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/script/generators/AkelosGenerator.php

http://akelosframework.googlecode.com/
PHP | 225 lines | 181 code | 29 blank | 15 comment | 19 complexity | 8b72377789eb6848d5d1a7124817f329 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Akelos Framework - http://www.akelos.org |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
  7. // | Released under the GNU Lesser General Public License, see LICENSE.txt|
  8. // +----------------------------------------------------------------------+
  9. /**
  10. * @package AkelosFramework
  11. * @subpackage Generators
  12. * @author Bermi Ferrer <bermi a.t akelos c.om>
  13. * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  14. * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  15. */
  16. class AkelosGenerator
  17. {
  18. var $log = array();
  19. var $type = '';
  20. var $_template_vars = array();
  21. var $collisions = array();
  22. function runCommand($command)
  23. {
  24. $commands = $this->getOptionsFromCommand($command);
  25. $generator_name = isset($commands['generator']) ? $commands['generator'] : array_shift($commands);
  26. if(empty($generator_name)){
  27. echo "\n ".Ak::t("You must supply a valid generator as the first command.\n\n Available generator are:");
  28. echo "\n\n ".join("\n ", $this->_getAvailableGenerators())."\n\n";
  29. AK_CONSOLE_MODE ? null : exit;
  30. return ;
  31. }
  32. if(count(array_diff($commands,array('help','-help','usage','-usage','h','-h','USAGE','-USAGE'))) != count($commands) || count($commands) == 0){
  33. $usage = method_exists($this,'banner') ? $this->banner() : @Ak::file_get_contents(AK_SCRIPT_DIR.DS.'generators'.DS.$generator_name.DS.'USAGE');
  34. echo empty($usage) ? "\n".Ak::t('Could not locate usage file for this generator') : "\n".$usage."\n";
  35. return;
  36. }
  37. if(file_exists(AK_SCRIPT_DIR.DS.'generators'.DS.$generator_name.DS.$generator_name.'_generator.php')){
  38. include_once(AK_SCRIPT_DIR.DS.'generators'.DS.$generator_name.DS.$generator_name.'_generator.php');
  39. $generator_class_name = AkInflector::camelize($generator_name.'_generator');
  40. $generator = new $generator_class_name();
  41. $generator->type = $generator_name;
  42. $generator->_identifyUnnamedCommands($commands);
  43. $generator->_assignVars($commands);
  44. $generator->cast();
  45. $generator->_generate();
  46. }else {
  47. echo "\n".Ak::t('Could not find %generator_name generator',array('%generator_name'=>$generator_name))."\n";
  48. }
  49. }
  50. function _assignVars($template_vars)
  51. {
  52. foreach ($template_vars as $key=>$value){
  53. $this->$key = $value;
  54. }
  55. $this->_template_vars = (array)$this;
  56. }
  57. function assignVarToTemplate($var_name, $value)
  58. {
  59. $this->_template_vars[$var_name] = $value;
  60. }
  61. function cast()
  62. {
  63. }
  64. function _identifyUnnamedCommands(&$commands)
  65. {
  66. $i = 0;
  67. $extra_commands = array();
  68. $unnamed_commands = array();
  69. foreach ($commands as $param=>$value){
  70. if($value[0] == '-'){
  71. $next_is_value_for = trim($value,'- ');
  72. $extra_commands[$next_is_value_for] = true;
  73. continue;
  74. }
  75. if(isset($next_is_value_for)){
  76. $extra_commands[$next_is_value_for] = trim($value,'- ');
  77. unset($next_is_value_for);
  78. continue;
  79. }
  80. if(is_numeric($param)){
  81. if(!empty($this->command_values[$i])){
  82. $index =$this->command_values[$i];
  83. if(substr($this->command_values[$i],0,7) == '(array)'){
  84. $index =substr($this->command_values[$i],7);
  85. $unnamed_commands[$index][] = $value;
  86. $i--;
  87. }else{
  88. $unnamed_commands[$index] = $value;
  89. }
  90. }
  91. $i++;
  92. }
  93. }
  94. $commands = array_merge($extra_commands, $unnamed_commands);
  95. }
  96. function render($template, $sintags_version = false)
  97. {
  98. extract($this->_template_vars);
  99. ob_start();
  100. include(AK_SCRIPT_DIR.DS.'generators'.DS.$this->type.DS.($sintags_version?'sintags_':'').'templates'.DS.(strstr($template,'.') ? $template : $template.'.tpl'));
  101. $result = ob_get_contents();
  102. ob_end_clean();
  103. return $result;
  104. }
  105. function save($file_path, $content)
  106. {
  107. $this->log[] = $file_path;
  108. Ak::file_put_contents($file_path, $content);
  109. }
  110. function printLog()
  111. {
  112. echo "\n".Ak::t('The following files have been created:')."\n";
  113. echo join("\n",$this->log)."\n";
  114. $this->log = array();
  115. }
  116. function _generate()
  117. {
  118. if(isset($this->_template_vars['force']) || !$this->hasCollisions()){
  119. $this->generate();
  120. $this->printLog();
  121. }else{
  122. echo "\n".Ak::t('There where collisions when attempting to generate the %type.',array('%type'=>$this->type))."\n";
  123. echo Ak::t('Please add force=true to the argument list in order to overwrite existing files.')."\n\n";
  124. echo join("\n",$this->collisions)."\n";
  125. }
  126. }
  127. function hasCollisions()
  128. {
  129. return false;
  130. }
  131. function getOptionsFromCommand($command)
  132. {
  133. $command = $this->_maskAmpersands($command);
  134. // Named params
  135. if(preg_match_all('/( ([A-Za-z0-9_-])+=)/',' '.$command,$result)){
  136. $command = str_replace($result[0],$this->_addAmpersands($result[0]),$command);
  137. if(preg_match_all('/( [A-Z-a-z0-9_-]+&)+/',' '.$command,$result)){
  138. $command = str_replace($result[0],$this->_addAmpersands($result[0]),$command);
  139. }
  140. }
  141. $command = join('&',array_diff(explode(' ',$command.' '),array('')));
  142. parse_str($command,$command_pieces);
  143. $command_pieces = array_map('stripslashes',$command_pieces);
  144. $command_pieces = array_map(array(&$this,'_unmaskAmpersands'),$command_pieces);
  145. $params = array();
  146. foreach ($command_pieces as $param=>$value){
  147. if(empty($value)){
  148. $params[] = $param;
  149. }else{
  150. $param = $param[0] == '-' ? substr($param,1) : $param;
  151. $params[$param] = trim($value,"\"\n\r\t");
  152. }
  153. }
  154. return $params;
  155. }
  156. function _addAmpersands($array)
  157. {
  158. $ret = array();
  159. foreach ($array as $arr){
  160. $ret[] = '&'.trim($arr);
  161. }
  162. return $ret;
  163. }
  164. function _maskAmpersands($str)
  165. {
  166. return str_replace('&','___AMP___',$str);
  167. }
  168. function _unmaskAmpersands($str)
  169. {
  170. return str_replace('___AMP___','&',$str);
  171. }
  172. function manifest()
  173. {
  174. return $this->generate();
  175. }
  176. function _getAvailableGenerators()
  177. {
  178. $generators = array();
  179. foreach (Ak::dir(AK_SCRIPT_DIR.DS.'generators',array('files'=>false,'dirs'=>true)) as $folder){
  180. $generator = array_shift(array_keys($folder));
  181. if(strstr($generator,'.php')){
  182. continue;
  183. }
  184. $generators[] = $generator;
  185. }
  186. return $generators;
  187. }
  188. }
  189. ?>