Skip to content
This repository was archived by the owner on Dec 7, 2019. It is now read-only.

[WIP] Write configuration when creating a controller #80

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ You can also generate the zftool.phar using the `bin/create-phar` command as rep
<path> The path to the root folder of the ZF2 application (optional)

### Controller creation:
zf.php create controller <name> <module> [<path>]
zf.php create controller <name> <module> [<path>] [--no-config]

<name> The name of the controller to be created
<module> The module in which the controller should be created
<path> The root path of a ZF2 application where to create the controller
--no-config Prevent that module configuration is updated

### Action creation:
zf.php create action <name> <controller> <module> [<path>]
Expand Down
2 changes: 1 addition & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
),
'zftool-create-controller' => array(
'options' => array(
'route' => 'create controller <name> <module> [<path>]',
'route' => 'create controller <name> <module> [<path>] [--no-config]',
'defaults' => array(
'controller' => 'ZFTool\Controller\Create',
'action' => 'controller',
Expand Down
79 changes: 78 additions & 1 deletion src/ZFTool/Controller/CreateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ public function controllerAction()
$request = $this->getRequest();
$name = $request->getParam('name');
$module = $request->getParam('module');
$path = $request->getParam('path', '.');
$path = rtrim($request->getParam('path'), '/');

$noConfig = $this->params()->fromRoute('no-config', false);

if (empty($path)) {
$path = '.';
}

if (!file_exists("$path/module") || !file_exists("$path/config/application.config.php")) {
return $this->sendError(
Expand Down Expand Up @@ -148,9 +154,80 @@ public function controllerAction()
$phtml = false;
$phtmlPath = $dir . "/index.phtml";
if (file_put_contents($phtmlPath, 'Action "index", controller "'.$ucName.'", module "'.$module.'".')) {
$console->writeLine(sprintf("Created view script at %s", $phtmlPath), Color::GREEN);
$phtml = true;
}

// check for no-config flag
if (!$noConfig) {
// Read module configuration
$moduleConfigNew = $moduleConfigOld = require "$path/module/$module/config/module.config.php";

// check for controllers configuration
if (!isset($moduleConfigNew['controllers'])) {
$moduleConfigNew['controllers'] = array();
}

// check for controllers invokables configuration
if (!isset($moduleConfigNew['controllers']['invokables'])) {
$moduleConfigNew['controllers']['invokables'] = array();
}

// define invokable key
$invokableKey = $module . '\Controller\\' . $name;

// check if invokable key is already there
if (!in_array($invokableKey, $moduleConfigNew['controllers']['invokables'])) {
$moduleConfigNew['controllers']['invokables'][$invokableKey] = ucfirst($module) . '\Controller\\' . $controller;
}

// check for view_manager
if (!isset($moduleConfigNew['view_manager'])) {
$moduleConfigNew['view_manager'] = array();
}

// check for template_path_stack
if (!isset($moduleConfigNew['view_manager']['template_path_stack'])) {
$moduleConfigNew['view_manager']['template_path_stack'] = array();
}

// set config dir
$configDir = realpath("$path/module/$module/config");

// check for any path
if (count($moduleConfigNew['view_manager']['template_path_stack']) > 0) {
// loop through path stack and add path again due to constant resolution problems
foreach ($moduleConfigNew['view_manager']['template_path_stack'] as $pathKey => $pathKey) {
if ($configDir . '/../view' == $pathKey) {
$moduleConfigNew['view_manager']['template_path_stack'][$pathKey] = '__DIR__ . \'/../view\'';
}
}
} else {
$moduleConfigNew['view_manager']['template_path_stack'][] = '__DIR__ . \'/../view\'';
}

// check for module config updates
if ($moduleConfigNew !== $moduleConfigOld) {
copy("$path/module/$module/config/module.config.php", "$path/module/$module/config/module.config.old");

$content = <<<EOD
<?php
/**
* Configuration file generated by ZFTool
* The previous configuration file is stored in module.config.old
*
* @see https://github.com/zendframework/ZFTool
*/

EOD;

$content .= 'return ' . Skeleton::exportConfig($moduleConfigNew) . ";\n";
file_put_contents("$path/module/$module/config/module.config.php", $content);

$console->writeLine("Module configuration was updated for module $module.", Color::YELLOW);
}
}

if (file_put_contents($ctrlPath, $file->generate()) && $phtml == true) {
$console->writeLine("The controller $name has been created in module $module.", Color::GREEN);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/ZFTool/Model/Skeleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static function exportConfig($config, $indent = 0)
{
if (empty(static::$valueGenerator)) {
static::$valueGenerator = new ValueGenerator();
static::$valueGenerator->initEnvironmentConstants();
}
static::$valueGenerator->setValue($config);
static::$valueGenerator->setArrayDepth($indent);
Expand Down
3 changes: 2 additions & 1 deletion src/ZFTool/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ public function getConsoleUsage(ConsoleAdapterInterface $console)
array('<path>', 'The root path of a ZF2 application where to create the module'),

'Controller creation:',
'create controller <name> <module> [<path>]' => 'create a controller in module',
'create controller <name> <module> [<path>] [--no-config]' => 'create a controller in module',
array('<name>', 'The name of the controller to be created'),
array('<module>', 'The module in which the controller should be created'),
array('<path>', 'The root path of a ZF2 application where to create the controller'),
array('--no-config', 'Prevent that module configuration is updated'),

'Action creation:',
'create action <name> <controllerName> <module> [<path>]' => 'create an action in a controller',
Expand Down