Fixes #149
[akelos.git] / lib / utils / generators / service / service_generator.php
blob272614c093e17d88aba90c4830d934b4c25c88b9
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
11 /**
12 * @package ActiveSupport
13 * @subpackage Generators
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
19 class ServiceGenerator extends AkelosGenerator
21 var $command_values = array('api_name');
23 var $api_methods;
24 var $api_method_doc;
26 function _preloadPaths()
28 $this->api_name = AkInflector::camelize($this->api_name);
29 $this->api_class_name = $this->api_name.'Api';
31 $this->assignVarToTemplate('api_class_name', $this->api_class_name);
33 $this->service_class_name = $this->api_name.'Service';
34 $this->assignVarToTemplate('service_class_name', $this->service_class_name);
36 $this->api_path = AK_APIS_DIR.DS.AkInflector::underscore($this->api_class_name).'.php';
38 $this->underscored_service_name = AkInflector::underscore($this->api_name);
39 $this->service_path = AK_MODELS_DIR.DS.$this->underscored_service_name.'_service.php';
42 function hasCollisions()
44 $this->_preloadPaths();
46 $this->collisions = array();
48 $files = array(
49 $this->service_path
51 foreach ($files as $file_name){
52 if(file_exists($file_name)){
53 $this->collisions[] = Ak::t('%file_name file already exists',array('%file_name'=>$file_name));
56 return count($this->collisions) > 0;
59 function _loadServiceStructureFromApi()
61 require_once(AK_LIB_DIR.DS.'AkActionWebService'.DS.'AkActionWebServiceApi.php');
62 require_once($this->api_path);
63 $Api =& new $this->api_class_name;
64 $api_methods =& $Api->getApiMethods();
65 $methods = array_keys($api_methods);
66 foreach ($methods as $method_name){
67 $this->api_methods[$method_name] = $this->_getFunctionParamsAsText($api_methods[$method_name]);
68 $this->_addDocBlock($api_methods[$method_name]);
71 $this->assignVarToTemplate('api_methods', $this->api_methods);
72 $this->assignVarToTemplate('api_method_doc', $this->api_method_doc);
75 function _getFunctionParamsAsText($ApiMethod)
77 $params = array();
78 foreach ($ApiMethod->expects as $k=>$param){
79 $params[] = "\$param_".($k+1)."_as_".$param;
81 return join(", ", $params);
84 function _addDocBlock($ApiMethod)
86 $this->api_method_doc[$ApiMethod->name] = !empty($ApiMethod->documentation)? "\n\t* ".$ApiMethod->documentation."\n\t*" : '';
87 foreach (array('expects', 'returns') as $expects_or_returns){
88 if(!empty($ApiMethod->{$expects_or_returns})){
89 //$this->api_method_doc[$ApiMethod->name] .= "\n\t* ".ucfirst($expects_or_returns).":";
90 foreach ($ApiMethod->{$expects_or_returns} as $k=>$type){
91 $this->api_method_doc[$ApiMethod->name] .= "\n\t* ".(
92 $expects_or_returns == 'expects' ?
93 '@param param'.($k+1) : '@return '
94 )." $type";
95 if(!empty($ApiMethod->{$expects_or_returns.'_documentation'}[$k])){
96 $this->api_method_doc[$ApiMethod->name] .= ' '.$ApiMethod->{$expects_or_returns.'_documentation'}[$k];
99 $this->api_method_doc[$ApiMethod->name] .= "\n\t* ";
105 function generate()
107 $this->_preloadPaths();
109 $this->_loadServiceStructureFromApi();
111 $files = array(
112 'service'=> $this->service_path
115 foreach ($files as $template=>$file_path){
116 $this->save($file_path, $this->render($template));