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