/** * @group pathspec */ public function testApiPathsPsr4() { $pathSpec = new ModulePathSpec($this->getModuleUtils(), 'psr-4'); $basePath = '/app/ModuleName/src/V2/'; $this->assertEquals($basePath . 'Rest/', $pathSpec->getRestPath('ModuleName', 2)); $this->assertEquals($basePath . 'Rest/ServiceName', $pathSpec->getRestPath('ModuleName', 2, 'ServiceName')); $this->assertEquals($basePath . 'Rpc/', $pathSpec->getRpcPath('ModuleName', 2)); $this->assertEquals($basePath . 'Rpc/ServiceName', $pathSpec->getRpcPath('ModuleName', 2, 'ServiceName')); }
/** * Create a controller in the current module named for the given service * * @param string $serviceName * @return object|false * @throws Exception\RuntimeException */ public function createController($serviceName) { $module = $this->module; $version = $this->moduleEntity->getLatestVersion(); $serviceName = str_replace("\\", "/", $serviceName); $srcPath = $this->modules->getRpcPath($module, $version, $serviceName); if (!file_exists($srcPath)) { mkdir($srcPath, 0775, true); } $className = sprintf('%sController', $serviceName); $classPath = sprintf('%s/%s.php', $srcPath, $className); $controllerService = sprintf('%s\\V%s\\Rpc\\%s\\Controller', $module, $version, $serviceName); if (file_exists($classPath)) { throw new Exception\RuntimeException(sprintf('The controller "%s" already exists', $className), 409); } $view = new ViewModel(['module' => $module, 'classname' => $className, 'servicename' => $serviceName, 'version' => $version]); $resolver = new Resolver\TemplateMapResolver(['code-connected/rpc-controller' => __DIR__ . '/../../view/code-connected/rpc-controller.phtml']); $view->setTemplate('code-connected/rpc-controller'); $renderer = new PhpRenderer(); $renderer->setResolver($resolver); if (!file_put_contents($classPath, "<" . "?php\n" . $renderer->render($view))) { return false; } $fullClassFactory = $this->createFactoryController($serviceName); $this->configResource->patch(['controllers' => ['factories' => [$controllerService => $fullClassFactory]]], true); $fullClassName = sprintf('%s\\V%s\\Rpc\\%s\\%s', $module, $version, $serviceName, $className); return (object) ['class' => $fullClassName, 'file' => $classPath, 'service' => $controllerService]; }
/** * Create a module * * @param string $module * @param string $path * @param integer $ver * @return boolean */ public function createModule($module, ModulePathSpec $pathSpec) { $path = $pathSpec->getApplicationPath(); $application = (require "{$path}/config/application.config.php"); if (is_array($application) && isset($application['modules']) && in_array($module, $application['modules'], true)) { // Module already exists in configuration return false; } $modulePath = $pathSpec->getModulePath($module, $path); if (file_exists($modulePath)) { throw new \Exception(sprintf('Cannot create new API module; module by the name "%s" already exists', $module), 409); } $moduleSourcePath = $pathSpec->getModuleSourcePath($module); $moduleSourceRelativePath = $pathSpec->getModuleSourcePath($module, false); $moduleConfigPath = $pathSpec->getModuleConfigPath($module); mkdir($moduleConfigPath, 0775, true); mkdir($pathSpec->getModuleViewPath($module), 0775, true); mkdir($pathSpec->getRestPath($module, 1), 0775, true); mkdir($pathSpec->getRpcPath($module, 1), 0775, true); if (!file_put_contents("{$moduleConfigPath}/module.config.php", "<" . "?php\nreturn array(\n);")) { return false; } $view = new ViewModel(['module' => $module]); $resolver = new Resolver\TemplateMapResolver(['module/skeleton' => __DIR__ . '/../../view/module/skeleton.phtml', 'module/skeleton-psr4' => __DIR__ . '/../../view/module/skeleton-psr4.phtml']); $renderer = new PhpRenderer(); $renderer->setResolver($resolver); if ($pathSpec->getPathSpec() === 'psr-0') { $view->setTemplate('module/skeleton'); $moduleRelClassPath = "{$moduleSourceRelativePath}/Module.php"; if (!file_put_contents("{$modulePath}/Module.php", "<" . "?php\nrequire __DIR__ . '{$moduleRelClassPath}';")) { return false; } if (!file_put_contents("{$moduleSourcePath}/Module.php", "<" . "?php\n" . $renderer->render($view))) { return false; } } else { $view->setTemplate('module/skeleton-psr4'); if (!file_put_contents("{$modulePath}/Module.php", "<" . "?php\n" . $renderer->render($view))) { return false; } } // Add the module in application.config.php if (is_array($application) && isset($application['modules']) && !in_array($module, $application['modules'], true)) { $application['modules'][] = $module; if (!$this->writeApplicationConfig($application, $path)) { return false; } } return true; }