ZF\Apigility\Admin\Model\ModuleModel::createModule PHP Method

createModule() public method

Create a module
public createModule ( string $module, ModulePathSpec $pathSpec ) : boolean
$module string
$pathSpec ModulePathSpec
return boolean
    public function createModule($module, ModulePathSpec $pathSpec)
    {
        $path = $pathSpec->getApplicationPath();
        $application = (require sprintf('%s/config/application.config.php', $path));
        if (is_array($application) && isset($application['modules']) && in_array($module, $application['modules'], true)) {
            // Module already exists in configuration
            return false;
        }
        $modulePath = $pathSpec->getModulePath($module);
        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);
        $payload = static::$useShortArrayNotation ? "[\n]" : "array(\n)";
        $payload = sprintf('<' . "?php\n return %s;", $payload);
        if (!file_put_contents(sprintf('%s/module.config.php', $moduleConfigPath), $payload)) {
            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() === ModulePathSpec::PSR_0) {
            $view->setTemplate('module/skeleton');
            $moduleRelClassPath = sprintf('%s/Module.php', $moduleSourceRelativePath);
            if (!file_put_contents(sprintf('%s/Module.php', $modulePath), "<" . "?php\nrequire __DIR__ . '{$moduleRelClassPath}';")) {
                return false;
            }
            if (!file_put_contents(sprintf('%s/Module.php', $moduleSourcePath), "<" . "?php\n" . $renderer->render($view))) {
                return false;
            }
        } else {
            $view->setTemplate('module/skeleton-psr4');
            if (!file_put_contents(sprintf('%s/Module.php', $modulePath), "<" . "?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;
    }

Usage Example

Beispiel #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;
    }