Adds PSR-0 and PSR-4 support to Apigility.
示例#1
0
 /**
  * @param  ModuleEntity $moduleEntity
  * @param  ModuleUtils $modules
  * @param  ConfigResource $config
  */
 public function __construct(ModuleEntity $moduleEntity, ModulePathSpec $modules, ConfigResource $config)
 {
     $this->module         = $moduleEntity->getName();
     $this->moduleEntity   = $moduleEntity;
     $this->modules        = $modules;
     $this->configResource = $config;
     $this->modulePath     = $modules->getModulePath($this->module);
 }
 /**
  * @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'));
 }
 /**
  * Set path to use when creating new modules
  *
  * @param  string $path
  * @return $this
  */
 public function setModulePath($path)
 {
     /*
      * maintain backwards compatibility
      * NOTE: modulePath in this case, is really the application path
      */
     $this->modulePathSpec->setApplicationPath($path);
     return $this;
 }
 /**
  * getDocsConfig
  * @param $module
  * @return null|\ZF\Configuration\ConfigResource
  * @deprecated
  */
 protected function getDocsConfig($module)
 {
     $moduleConfigPath = $this->moduleUtils->getModuleConfigPath($module);
     $docConfigPath = dirname($moduleConfigPath) . '/documentation.config.php';
     if (!file_exists($docConfigPath)) {
         return null;
     }
     $documentation = (include $docConfigPath);
     return $this->configFactory->createConfigResource($documentation, $docConfigPath);
 }
 /**
  * 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];
 }
 /**
  * Get the source path for the module
  *
  * @param string $resourceName
  * @return string
  */
 protected function getSourcePath($resourceName)
 {
     $sourcePath = $this->modules->getRestPath($this->module, $this->moduleEntity->getLatestVersion(), $resourceName);
     if (!file_exists($sourcePath)) {
         mkdir($sourcePath, 0777, true);
     }
     return $sourcePath;
 }
 /**
  * @param  string $name
  * @return string
  * @deprecated
  */
 protected function normalizeModuleName($name)
 {
     return $this->modules->normalizeModuleName($name);
 }
 /**
  * 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;
 }
 /**
  * createWithPathSpec
  *
  * @param string $moduleName
  * @param ModulePathSpec $pathSpec
  * @param ConfigResource $config
  * @param ConfigResource|null $docsConfig
  *
  * @return static
  */
 public static function createWithPathSpec($moduleName, ModulePathSpec $pathSpec, ConfigResource $config, ConfigResource $docsConfig = null)
 {
     $moduleName = $pathSpec->normalizeModuleName((string) $moduleName);
     return new static($moduleName, $pathSpec->getModuleConfigPath($moduleName), $pathSpec->getModuleSourcePath($moduleName), $config, $docsConfig, $pathSpec->getPathSpec());
 }