| 17 | | if(count($args) < 3) |
|---|
| 18 | | { |
|---|
| 19 | | throw new Exception('you must provide the webservice name'); |
|---|
| 20 | | } |
|---|
| 21 | | |
|---|
| 22 | | if(count($args) < 4) |
|---|
| 23 | | { |
|---|
| 24 | | throw new Exception('you must provide the webservice url'); |
|---|
| 25 | | } |
|---|
| 26 | | |
|---|
| 27 | | $last_index = count($args)-1; |
|---|
| 28 | | |
|---|
| 29 | | $sf_app = $args[0]; |
|---|
| 30 | | $sf_env = $args[1]; |
|---|
| 31 | | $ws_name = $args[$last_index-1]; |
|---|
| 32 | | unset($args[$last_index-1]); |
|---|
| 33 | | $ws_url = $args[$last_index]; |
|---|
| 34 | | unset($args[$last_index]); |
|---|
| 35 | | |
|---|
| 36 | | $sf_controller = isset($args[2]) ? $args[2] : $sf_app.'_'.$sf_env; |
|---|
| 37 | | |
|---|
| 38 | | run_init_controller($task, $args); |
|---|
| 39 | | |
|---|
| 40 | | sfConfig::set('sf_app_dir', sfConfig::get('sf_root_dir').'/apps/'.$sf_app); |
|---|
| 41 | | sfConfig::set('sf_app_lib_dir', sfConfig::get('sf_app_dir').'/lib'); |
|---|
| 42 | | _register_lib_dirs(); |
|---|
| 43 | | |
|---|
| 44 | | $root_modules_dir = sfConfig::get('sf_root_dir').'/apps/'.$sf_app.'/modules'; |
|---|
| 45 | | $finder = pakeFinder::type('directory')->name('*')->relative()->maxdepth(0); |
|---|
| 46 | | |
|---|
| 47 | | $ws_def = new WsdlDefinition(); |
|---|
| 48 | | $ws_def->setDefinitionName($ws_name); |
|---|
| 49 | | $ws_def->setWsdlFileName(sfConfig::get('sf_root_dir').'/web/'.$ws_name.'.wsdl'); |
|---|
| 50 | | $ws_def->setNameSpace($ws_url); |
|---|
| 51 | | $ws_def->setEndPoint($ws_url.$sf_controller.'.php'); |
|---|
| 52 | | |
|---|
| 53 | | $ws_write = new WsdlWriter($ws_def); |
|---|
| 54 | | |
|---|
| 55 | | $methods = array(); |
|---|
| 56 | | |
|---|
| 57 | | foreach($finder->in($root_modules_dir) as $module_dir) |
|---|
| 58 | | { |
|---|
| 59 | | //proposed by Nicolas Martin to avoid problems with 'inited' modules |
|---|
| 60 | | if(!preg_match('/class(.*)Actions(.*)extends(.*)auto/', file_get_contents($root_modules_dir.'/'.$module_dir.'/actions/actions.class.php')) && file_exists($root_modules_dir.'/'.$module_dir.'/actions/actions.class.php')) |
|---|
| 61 | | { |
|---|
| 62 | | require_once($root_modules_dir.'/'.$module_dir.'/actions/actions.class.php'); |
|---|
| 63 | | |
|---|
| 64 | | $class = new ReflectionClass($module_dir.'Actions'); |
|---|
| 65 | | |
|---|
| 66 | | $module_config = $root_modules_dir.'/'.$module_dir.'/config/module.yml'; |
|---|
| 67 | | |
|---|
| 68 | | if(file_exists($module_config)) |
|---|
| | 21 | const CONTROLLER_TEMPLATE_PATH = '/task/generator/skeleton/app/web/index.php'; |
|---|
| | 22 | |
|---|
| | 23 | /** |
|---|
| | 24 | * @see sfTask |
|---|
| | 25 | */ |
|---|
| | 26 | protected function configure() |
|---|
| | 27 | { |
|---|
| | 28 | $this->namespace = 'webservice'; |
|---|
| | 29 | $this->name = 'generate-wsdl'; |
|---|
| | 30 | $this->briefDescription = 'Generate a wsdl file from your marked module actions to expose them as a webservice api'; |
|---|
| | 31 | $this->detailedDescription = <<<EOF |
|---|
| | 32 | The [webservice:generate-wsdl|INFO] task generates a wsdl file from your marked module actions to expose them as a webservice api. |
|---|
| | 33 | Call it with: |
|---|
| | 34 | |
|---|
| | 35 | [./symfony webservice:generate-wsdl|INFO] |
|---|
| | 36 | |
|---|
| | 37 | This task also creates a front controller script in the [web/|COMMENT] directory: |
|---|
| | 38 | |
|---|
| | 39 | [web/%application%_%environment%.php|INFO] |
|---|
| | 40 | |
|---|
| | 41 | You can set the environment by using the [environment|COMMENT] option: |
|---|
| | 42 | |
|---|
| | 43 | [./symfony webservice:generate-wsdl frontend --environment=soap|INFO] |
|---|
| | 44 | |
|---|
| | 45 | or |
|---|
| | 46 | |
|---|
| | 47 | [./symfony webservice:generate-wsdl frontend -e=soap|INFO] |
|---|
| | 48 | |
|---|
| | 49 | You can enable debugging for the controller by using the [debug|COMMENT] option: |
|---|
| | 50 | |
|---|
| | 51 | [./symfony webservice:generate-wsdl frontend --debug=on|INFO] |
|---|
| | 52 | |
|---|
| | 53 | or |
|---|
| | 54 | |
|---|
| | 55 | [./symfony webservice:generate-wsdl frontend -d=on|INFO] |
|---|
| | 56 | |
|---|
| | 57 | EOF; |
|---|
| | 58 | |
|---|
| | 59 | $this->addArgument('application', sfCommandArgument::REQUIRED, 'The application name'); |
|---|
| | 60 | $this->addArgument('name', sfCommandArgument::REQUIRED, 'The webservice name'); |
|---|
| | 61 | $this->addArgument('url', sfCommandArgument::REQUIRED, 'The webservice url base'); |
|---|
| | 62 | |
|---|
| | 63 | $this->addOption('environment', 'e', sfCommandOption::PARAMETER_REQUIRED, 'The environment to use for webservice mode', 'soap'); |
|---|
| | 64 | $this->addOption('debug', 'd', sfCommandOption::PARAMETER_NONE, 'Enables debugging in generated controller'); |
|---|
| | 65 | } |
|---|
| | 66 | |
|---|
| | 67 | /** |
|---|
| | 68 | * @see sfTask |
|---|
| | 69 | */ |
|---|
| | 70 | protected function doRun(sfCommandManager $commandManager, $options) |
|---|
| | 71 | { |
|---|
| | 72 | $this->registerPluginLibs(); |
|---|
| | 73 | |
|---|
| | 74 | $this->process($commandManager, $options); |
|---|
| | 75 | |
|---|
| | 76 | $this->checkProjectExists(); |
|---|
| | 77 | |
|---|
| | 78 | $app = $commandManager->getArgumentValue('application'); |
|---|
| | 79 | $this->checkAppExists($app); |
|---|
| | 80 | sfConfig::set('sf_app_module_dir', sprintf('%s/../../apps/%s/modules', $this->getPluginDir(), $app)); |
|---|
| | 81 | |
|---|
| | 82 | return $this->execute($commandManager->getArgumentValues(), $commandManager->getOptionValues()); |
|---|
| | 83 | } |
|---|
| | 84 | |
|---|
| | 85 | /** |
|---|
| | 86 | * @see sfTask |
|---|
| | 87 | */ |
|---|
| | 88 | protected function execute($arguments = array(), $options = array()) |
|---|
| | 89 | { |
|---|
| | 90 | $app = $arguments['application']; |
|---|
| | 91 | $env = $options['environment']; |
|---|
| | 92 | $dbg = $options['debug']; |
|---|
| | 93 | $name = $arguments['name']; |
|---|
| | 94 | $url = $arguments['url']; |
|---|
| | 95 | $url = '/' == substr($url, strlen($url) - 1) ? $url : $url.'/'; |
|---|
| | 96 | |
|---|
| | 97 | $controller_name = $name.'.php'; |
|---|
| | 98 | $controller_path = sprintf('%s/%s', sfConfig::get('sf_web_dir'), $controller_name); |
|---|
| | 99 | |
|---|
| | 100 | $this->getFilesystem()->remove($controller_path); |
|---|
| | 101 | $this->getFilesystem()->copy(sfConfig::get('sf_symfony_lib_dir').self::CONTROLLER_TEMPLATE_PATH, $controller_path); |
|---|
| | 102 | |
|---|
| | 103 | $this->getFilesystem()->replaceTokens($controller_path, '##', '##', array( |
|---|
| | 104 | 'APP_NAME' => $app, |
|---|
| | 105 | 'ENVIRONMENT' => $env, |
|---|
| | 106 | 'IS_DEBUG' => $dbg ? 'true' : 'false', |
|---|
| | 107 | )); |
|---|
| | 108 | |
|---|
| | 109 | $finder = sfFinder::type('directory')->name('*')->relative()->maxdepth(0); |
|---|
| | 110 | |
|---|
| | 111 | $ws_def = new WsdlDefinition(); |
|---|
| | 112 | $ws_def->setDefinitionName($name); |
|---|
| | 113 | $ws_def->setWsdlFileName(sprintf('%s/%s.wsdl', sfConfig::get('sf_web_dir'), $name)); |
|---|
| | 114 | $ws_def->setNameSpace($url); |
|---|
| | 115 | $ws_def->setEndPoint($url.$controller_name); |
|---|
| | 116 | |
|---|
| | 117 | $ws_write = new WsdlWriter($ws_def); |
|---|
| | 118 | |
|---|
| | 119 | $methods = array(); |
|---|
| | 120 | |
|---|
| | 121 | foreach($finder->in(sfConfig::get('sf_app_module_dir')) as $module_dir) |
|---|
| | 122 | { |
|---|
| | 123 | // proposed by Nicolas Martin to avoid problems with 'inited' modules |
|---|
| | 124 | if(!preg_match('/class(.*)Actions(.*)extends(.*)auto/', file_get_contents(sfConfig::get('sf_app_module_dir').'/'.$module_dir.'/actions/actions.class.php')) && file_exists(sfConfig::get('sf_app_module_dir').'/'.$module_dir.'/actions/actions.class.php')) |
|---|
| 70 | | $yml = sfYaml::load($module_config); |
|---|
| | 126 | require_once(sfConfig::get('sf_app_module_dir').'/'.$module_dir.'/actions/actions.class.php'); |
|---|
| | 127 | |
|---|
| | 128 | $class = new ReflectionClass($module_dir.'Actions'); |
|---|
| | 129 | |
|---|
| | 130 | $module_config = sfConfig::get('sf_app_module_dir').'/'.$module_dir.'/config/module.yml'; |
|---|
| | 131 | |
|---|
| | 132 | if(!file_exists($module_config)) |
|---|
| | 133 | { |
|---|
| | 134 | $this->getFilesystem()->touch($module_config); |
|---|
| | 135 | } |
|---|
| | 136 | |
|---|
| | 137 | $yml = sfYaml::load($module_config); |
|---|
| | 138 | |
|---|
| | 139 | if(!isset($yml[$env]) || !is_array($yml[$env])) |
|---|
| | 140 | { |
|---|
| | 141 | $yml[$env] = array(); |
|---|
| | 142 | } |
|---|
| | 143 | |
|---|
| | 144 | $yml[$env]['soap_parameter_map'] = array(); |
|---|
| | 145 | |
|---|
| | 146 | foreach($class->getMethods() as $method) |
|---|
| | 147 | { |
|---|
| | 148 | $name = $method->getName(); |
|---|
| | 149 | |
|---|
| | 150 | if(substr($name,0,7)=='execute' && strlen($name)>7) |
|---|
| | 151 | { |
|---|
| | 152 | $action = strtolower(substr($name, 7, 1)).substr($name, 8); |
|---|
| | 153 | $name = $module_dir.'_'.$action; |
|---|
| | 154 | |
|---|
| | 155 | $param_return = $this->parseMethodCommentBlock($method->getDocComment()); |
|---|
| | 156 | |
|---|
| | 157 | if($param_return == null) |
|---|
| | 158 | { |
|---|
| | 159 | continue; |
|---|
| | 160 | } |
|---|
| | 161 | |
|---|
| | 162 | $yml[$env]['soap_parameter_map'][$action] = array(); |
|---|
| | 163 | |
|---|
| | 164 | $ws_method = new WsdlMethod(); |
|---|
| | 165 | $ws_method->setName($name); |
|---|
| | 166 | |
|---|
| | 167 | if(!is_null($param_return['return'] && !empty($param_return['return']))) |
|---|
| | 168 | { |
|---|
| | 169 | $ws_method->setReturn($param_return['return']['type'], $param_return['return']['desc']); |
|---|
| | 170 | } |
|---|
| | 171 | |
|---|
| | 172 | foreach($param_return['param'] as $param) |
|---|
| | 173 | { |
|---|
| | 174 | $yml[$env]['soap_parameter_map'][$action][] = $param['name']; |
|---|
| | 175 | |
|---|
| | 176 | $ws_method->addParameter($param['type'], $param['name'], $param['desc']); |
|---|
| | 177 | } |
|---|
| | 178 | |
|---|
| | 179 | $methods[] = $ws_method; |
|---|
| | 180 | |
|---|
| | 181 | $ws_write->addMethod($ws_method); |
|---|
| | 182 | } |
|---|
| | 183 | } |
|---|
| | 184 | |
|---|
| | 185 | // only save if we added something to the configuration |
|---|
| | 186 | if(!empty($yml[$env]['soap_parameter_map'])) |
|---|
| | 187 | { |
|---|
| | 188 | file_put_contents($module_config, sfYaml::dump($yml)); |
|---|
| | 189 | } |
|---|
| | 190 | |
|---|
| 72 | | else |
|---|
| | 192 | } |
|---|
| | 193 | |
|---|
| | 194 | $complexTypes = WsdlType::getComplexTypes($methods); |
|---|
| | 195 | |
|---|
| | 196 | foreach ($complexTypes as &$complexType) |
|---|
| | 197 | { |
|---|
| | 198 | $ws_write->addComplexType($complexType); |
|---|
| | 199 | } |
|---|
| | 200 | |
|---|
| | 201 | $ws_write->doCreateWsdl(); |
|---|
| | 202 | $ws_write->save(); |
|---|
| | 203 | } |
|---|
| | 204 | |
|---|
| | 205 | /** |
|---|
| | 206 | * Returns the plugin root path. |
|---|
| | 207 | * |
|---|
| | 208 | * @return string The plugin root path |
|---|
| | 209 | */ |
|---|
| | 210 | protected function getPluginDir() |
|---|
| | 211 | { |
|---|
| | 212 | return dirname(__FILE__).'/../..'; |
|---|
| | 213 | } |
|---|
| | 214 | |
|---|
| | 215 | /** |
|---|
| | 216 | * Registers required class files for autoloading. |
|---|
| | 217 | * |
|---|
| | 218 | */ |
|---|
| | 219 | protected function registerPluginLibs() |
|---|
| | 220 | { |
|---|
| | 221 | $autoload = sfSimpleAutoload::getInstance(); |
|---|
| | 222 | $autoload->addDirectory($this->getPluginDir().'/lib/vendor/wsdl'); |
|---|
| | 223 | $autoload->addDirectory($this->getPluginDir().'/lib/util'); |
|---|
| | 224 | } |
|---|
| | 225 | |
|---|
| | 226 | /** |
|---|
| | 227 | * Parses parameter and return types, names and descriptions from a method comment block, if the tag @ws-enable is found. |
|---|
| | 228 | * |
|---|
| | 229 | * @param string $text A method comment block |
|---|
| | 230 | * @return array An array containing parameter and return types, names and descriptions |
|---|
| | 231 | */ |
|---|
| | 232 | protected function parseMethodCommentBlock($text) |
|---|
| | 233 | { |
|---|
| | 234 | $result = array('param'=>array(), 'return'=>null); |
|---|
| | 235 | $lines = explode("\n", $text); |
|---|
| | 236 | |
|---|
| | 237 | $enable = false; |
|---|
| | 238 | |
|---|
| | 239 | foreach($lines as $line) |
|---|
| | 240 | { |
|---|
| | 241 | $line = trim($line); |
|---|
| | 242 | |
|---|
| | 243 | if(substr($line, 0, 2) == '* ' && substr($line, 2, 1) == '@') |
|---|
| 76 | | |
|---|
| 77 | | if(!is_array($yml[$sf_env])) |
|---|
| 78 | | { |
|---|
| 79 | | $yml[$sf_env] = array(); |
|---|
| 80 | | } |
|---|
| 81 | | |
|---|
| 82 | | $yml[$sf_env]['soap_parameter_map'] = array(); |
|---|
| 83 | | |
|---|
| 84 | | foreach($class->getMethods() as $method) |
|---|
| 85 | | { |
|---|
| 86 | | $name = $method->getName(); |
|---|
| 87 | | |
|---|
| 88 | | if(substr($name,0,7)=='execute' && strlen($name)>7) |
|---|
| 89 | | { |
|---|
| 90 | | $action = strtolower(substr($name, 7, 1)).substr($name, 8); |
|---|
| 91 | | $name = $module_dir.'_'.$action; |
|---|
| 92 | | |
|---|
| 93 | | $param_return = _parse_method_comment($method->getDocComment()); |
|---|
| 94 | | |
|---|
| 95 | | if($param_return == null) |
|---|
| 96 | | { |
|---|
| 97 | | continue; |
|---|
| 98 | | } |
|---|
| 99 | | |
|---|
| 100 | | pake_echo_action('method+', $name); |
|---|
| 101 | | |
|---|
| 102 | | $yml[$sf_env]['soap_parameter_map'][$action] = array(); |
|---|
| 103 | | |
|---|
| 104 | | $ws_method = new WsdlMethod(); |
|---|
| 105 | | $ws_method->setName($name); |
|---|
| 106 | | |
|---|
| 107 | | if(!is_null($param_return['return'] && !empty($param_return['return']))) |
|---|
| 108 | | { |
|---|
| 109 | | $ws_method->setReturn($param_return['return']['type'], $param_return['return']['desc']); |
|---|
| 110 | | } |
|---|
| 111 | | |
|---|
| 112 | | foreach($param_return['param'] as $param) |
|---|
| 113 | | { |
|---|
| 114 | | $yml[$sf_env]['soap_parameter_map'][$action][] = $param['name']; |
|---|
| 115 | | |
|---|
| 116 | | $ws_method->addParameter($param['type'], $param['name'], $param['desc']); |
|---|
| 117 | | } |
|---|
| 118 | | |
|---|
| 119 | | $methods[] = $ws_method; |
|---|
| 120 | | |
|---|
| 121 | | $ws_write->addMethod($ws_method); |
|---|
| 122 | | } |
|---|
| 123 | | } |
|---|
| 124 | | |
|---|
| 125 | | //only save if we added something to the configuration |
|---|
| 126 | | if(!empty($yml[$sf_env]['soap_parameter_map'])) |
|---|
| 127 | | { |
|---|
| 128 | | pake_echo_action('file+', $module_config); |
|---|
| 129 | | file_put_contents($module_config, sfYaml::dump($yml)); |
|---|
| 130 | | } |
|---|
| 131 | | |
|---|
| 132 | | } |
|---|
| 133 | | } |
|---|
| 134 | | |
|---|
| 135 | | $complexTypes = WsdlType::getComplexTypes($methods); |
|---|
| 136 | | |
|---|
| 137 | | foreach ($complexTypes as &$complexType) |
|---|
| 138 | | { |
|---|
| 139 | | $ws_write->addComplexType($complexType); |
|---|
| 140 | | } |
|---|
| 141 | | |
|---|
| 142 | | $ws_write->doCreateWsdl(); |
|---|
| 143 | | $ws_write->save(); |
|---|
| 144 | | pake_echo_action('file+', $ws_def->getWsdlFileName()); |
|---|
| | 263 | } |
|---|
| | 264 | |
|---|
| | 265 | if($enable) |
|---|
| | 266 | { |
|---|
| | 267 | return $result; |
|---|
| | 268 | } |
|---|
| | 269 | else |
|---|
| | 270 | { |
|---|
| | 271 | return null; |
|---|
| | 272 | } |
|---|
| | 273 | } |
|---|
| 146 | | |
|---|
| 147 | | function _register_lib_dirs() |
|---|
| 148 | | { |
|---|
| 149 | | //hack to init simpleAutoLoader |
|---|
| 150 | | __autoload('sfConfig'); |
|---|
| 151 | | simpleAutoLoader::register(sfConfig::get('sf_lib_dir'), '.class.php'); |
|---|
| 152 | | simpleAutoLoader::register(sfConfig::get('sf_app_lib_dir'), '.class.php'); |
|---|
| 153 | | simpleAutoLoader::register(sfConfig::get('sf_root_dir').'/plugins/modules/*/actions', '.class.php'); |
|---|
| 154 | | simpleAutoLoader::register(sfConfig::get('sf_app_dir').'/modules/*/actions', '.class.php'); |
|---|
| 155 | | simpleAutoLoader::register(sfConfig::get('sf_app_dir').'/modules/*/lib', '.class.php'); |
|---|
| 156 | | |
|---|
| 157 | | $finder = pakeFinder::type('directory')->name('*'); |
|---|
| 158 | | |
|---|
| 159 | | foreach($finder->in(sfConfig::get('sf_lib_dir')) as $dir) |
|---|
| 160 | | { |
|---|
| 161 | | simpleAutoLoader::register($dir, '.php'); |
|---|
| 162 | | } |
|---|
| 163 | | |
|---|
| 164 | | foreach($finder->in(sfConfig::get('sf_app_lib_dir')) as $dir) |
|---|
| 165 | | { |
|---|
| 166 | | simpleAutoLoader::register($dir, '.php'); |
|---|
| 167 | | } |
|---|
| 168 | | |
|---|
| 169 | | } |
|---|
| 170 | | |
|---|
| 171 | | function _parse_method_comment($comment) |
|---|
| 172 | | { |
|---|
| 173 | | $result = array('param'=>array(), 'return'=>null); |
|---|
| 174 | | $lines = explode("\n", $comment); |
|---|
| 175 | | |
|---|
| 176 | | $enable = false; |
|---|
| 177 | | |
|---|
| 178 | | foreach($lines as $line) |
|---|
| 179 | | { |
|---|
| 180 | | $line = trim($line); |
|---|
| 181 | | |
|---|
| 182 | | if(substr($line, 0, 2) == '* ' && substr($line, 2, 1) == '@') |
|---|
| 183 | | { |
|---|
| 184 | | $parts = explode(' ', substr($line, 3), 4); |
|---|
| 185 | | |
|---|
| 186 | | if($parts[0] == 'ws-enable') |
|---|
| 187 | | { |
|---|
| 188 | | $enable = true; |
|---|
| 189 | | } |
|---|
| 190 | | else if($parts[0] == 'param' && count($parts)>=3) |
|---|
| 191 | | { |
|---|
| 192 | | $desc = isset($parts[3]) ? $parts[3] : ''; |
|---|
| 193 | | $result[$parts[0]][] = array('name'=>substr($parts[2], 1), 'type'=>$parts[1], 'desc'=>$desc); |
|---|
| 194 | | } |
|---|
| 195 | | else if($parts[0] == 'return' && count($parts)>=2) |
|---|
| 196 | | { |
|---|
| 197 | | $desc = isset($parts[2]) ? $parts[2] : ''; |
|---|
| 198 | | $desc .= isset($parts[3]) ? ' '.$parts[3] : ''; |
|---|
| 199 | | $result[$parts[0]] = array('type'=>$parts[1], 'desc'=>$desc); |
|---|
| 200 | | } |
|---|
| 201 | | } |
|---|
| 202 | | } |
|---|
| 203 | | |
|---|
| 204 | | if($enable) |
|---|
| 205 | | { |
|---|
| 206 | | return $result; |
|---|
| 207 | | } |
|---|
| 208 | | else |
|---|
| 209 | | { |
|---|
| 210 | | return null; |
|---|
| 211 | | } |
|---|
| 212 | | } |
|---|