Ejemplo n.º 1
0
    /**
     * Create a new API-First enabled module
     *
     * @param  array|object $data
     * @return ModuleEntity
     * @throws CreationException
     */
    public function create($data)
    {
        if (is_object($data)) {
            $data = (array) $data;
        }

        if (!isset($data['name'])) {
            throw new CreationException('Missing module name');
        }

        $version = isset($data['version']) ? $data['version'] : 1;
        $name    = $data['name'];
        $name    = str_replace('.', '\\', $name);
        if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]*(\\\+[a-zA-Z][a-zA-Z0-9_]*)?$/', $name)) {
            throw new CreationException('Invalid module name; must be a valid PHP namespace name');
        }

        if (false === $this->modules->createModule($name, $this->modulePathSpec)) {
            throw new CreationException('Unable to create module; check your paths and permissions');
        }

        $metadata = new ModuleEntity($name);
        $metadata->exchangeArray(array(
            'versions' => array($version),
        ));
        return $metadata;
    }
Ejemplo n.º 2
0
    public function testCanSetAndRetrieveModuleDefaultVersion()
    {
        $moduleEntity = new ModuleEntity('Test\Foo');
        $this->assertSame(1, $moduleEntity->getDefaultVersion()); // initial state

        $moduleEntity->exchangeArray(array('default_version' => 123));
        $this->assertSame(123, $moduleEntity->getDefaultVersion());
    }
Ejemplo n.º 3
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);
 }
 /**
  * Create a controller in the current module named for the given service
  *
  * @param  string $serviceName
  * @return mixed
  */
 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));
     }
     $view = new ViewModel(array('module' => $module, 'classname' => $className, 'servicename' => $serviceName, 'version' => $version));
     $resolver = new Resolver\TemplateMapResolver(array('code-connected/rpc-controller' => __DIR__ . '/../../../view/doctrine/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;
     }
     $fullClassName = sprintf('%s\\V%s\\Rpc\\%s\\%s', $module, $version, $serviceName, $className);
     $this->configResource->patch(array('controllers' => array('invokables' => array($controllerService => $fullClassName))), true);
     return (object) array('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;
 }
 /**
  * Get the source path for the module
  *
  * @param  string $serviceName
  * @return string
  */
 protected function getSourcePath($serviceName)
 {
     $sourcePath = sprintf('%s/src/%s/V%s/Rest/%s', $this->modulePath, str_replace('\\', '/', $this->module), $this->moduleEntity->getLatestVersion(), $serviceName);
     if (!file_exists($sourcePath)) {
         mkdir($sourcePath, 0775, true);
     }
     return $sourcePath;
 }
Ejemplo n.º 7
0
 /**
  * Create the mediatype for this
  *
  * Based on the module and the latest module version.
  *
  * @return string
  */
 public function createMediaType()
 {
     return sprintf(
         'application/vnd.%s.v%s+json',
         $this->normalize($this->module),
         $this->moduleEntity->getLatestVersion()
     );
 }
Ejemplo n.º 8
0
 /**
  * Inject relational links into a Module resource
  *
  * @param Model\ModuleEntity $resource
  * @param \Zend\Mvc\MvcEvent $e
  */
 public function injectModuleCollectionRelationalLinks(Model\ModuleEntity $resource, $e)
 {
     $asArray = $resource->getArrayCopy();
     $module = $asArray['name'];
     $rest = $asArray['rest'];
     $rpc = $asArray['rpc'];
     unset($asArray['rest']);
     unset($asArray['rpc']);
     $halEntity = new Entity($asArray, $module);
     $links = $halEntity->getLinks();
     $links->add(Link::factory(array('rel' => 'self', 'route' => array('name' => 'zf-apigility/api/module', 'params' => array('name' => $module)))));
     $this->injectLinksForServicesByType('authorization', array(), $links, $module);
     $this->injectLinksForServicesByType('rest', $rest, $links, $module);
     $this->injectLinksForServicesByType('rpc', $rpc, $links, $module);
     $e->setParam('entity', $halEntity);
 }
Ejemplo n.º 9
0
 /**
  * Returns list of all API-First-enabled modules
  *
  * @return array
  */
 protected function getEnabledModules()
 {
     if (is_array($this->modules)) {
         return $this->modules;
     }
     $this->modules = array();
     foreach ($this->moduleManager->getLoadedModules() as $moduleName => $module) {
         if (!$module instanceof ApigilityProviderInterface && !$module instanceof ApigilityModuleInterface) {
             continue;
         }
         if ($module instanceof ApigilityModuleInterface) {
             trigger_error('ZF\\Apigility\\ApigilityModuleInterface is deprecated,
                 use ZF\\Apigility\\Provider\\ApigilityProviderInterface instead', E_USER_DEPRECATED);
         }
         $services = $this->getServicesByModule($moduleName);
         $versions = $this->getVersionsByModule($moduleName, $module);
         $entity = new ModuleEntity($moduleName, $services['rest'], $services['rpc']);
         $entity->exchangeArray(array('versions' => $versions, 'default_version' => $this->getModuleDefaultVersion($module)));
         $this->modules[$entity->getName()] = $entity;
     }
     return $this->modules;
 }
 /**
  * @param Model\ModuleEntity $module
  * @param $links
  * @param HalJsonModel $model
  * @return void
  */
 private function injectModuleResourceRelationalLinks(Model\ModuleEntity $module, $links, HalJsonModel $model)
 {
     $moduleData = $module->getArrayCopy();
     $moduleName = $moduleData['name'];
     $this->injectLinksByType('authorization', $links, $moduleName);
     $this->injectLinksByType('rest', $links, $moduleName);
     unset($moduleData['rest']);
     $this->injectLinksByType('rpc', $links, $moduleName);
     unset($moduleData['rpc']);
     $module = new Model\ModuleEntity($module->getNamespace(), [], [], $module->isVendor());
     $module->exchangeArray($moduleData);
     $replacement = new Entity($module, $moduleName);
     $replacement->setLinks($links);
     $model->setPayload($replacement);
 }