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

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/ZFTool into f…
Browse files Browse the repository at this point in the history
…eature/zend-diagnostics-refactor
  • Loading branch information
Thinkscape committed Mar 8, 2014
2 parents a2e1687 + d73bf6f commit 567be92
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 43 deletions.
30 changes: 19 additions & 11 deletions README.md
Expand Up @@ -10,6 +10,7 @@ It runs from the command line and can be installed as ZF2 module or as PHAR (see
* Create a new project (install the ZF2 skeleton application)
* Create a new module
* Create a new controller
* Create a new action in a controller
* [Application diagnostics](docs/DIAGNOSTICS.md)

## Requirements
Expand Down Expand Up @@ -59,19 +60,27 @@ You can also generate the zftool.phar using the `bin/create-phar` command as rep

<name> The name of the module to be created
<path> The path to the root folder of the ZF2 application (optional)

### Controller creation
zf.php create controller <name> <module>
<name> The name of the controller to be created
<module> The module in which the controller should be created

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

<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

### Action creation:
zf.php create action <name> <controller> <module> [<path>]

<name> The name of the action to be created
<controller> The name of the controller in which the action should be created
<module> The module containing the controller
<path> The root path of a ZF2 application where to create the action

### Application configuration

zf.php config list list all configuration option
zf.php config get <name> display a single config value, i.e. "config get db.host"
zf.php config set <name> <value> set a single config value (use only to change scalar values)
zf.php config get <name> display a single config value, i.e. "config get db.host"
zf.php config set <name> <value> set a single config value (use only to change scalar values)

### Classmap generator

Expand Down Expand Up @@ -112,4 +121,3 @@ Note: If the above fails due to permissions, run the mv line again with sudo.
* Inspection of application configuration. [DONE]
* Deploying zf2 skeleton applications. [DONE]
* Reading and writing app configuration. [DONE]

4 changes: 2 additions & 2 deletions bin/create-phar
Expand Up @@ -4,7 +4,7 @@
* Create zftool.phar
*
* @link http://github.com/zendframework/ZFTool for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down Expand Up @@ -41,7 +41,7 @@ $stub = <<<EOF
* This file is part of ZFTool command line tool
*
* @link http://github.com/zendframework/ZFTool for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
Phar::mapPhar('$filename');
Expand Down
11 changes: 10 additions & 1 deletion config/module.config.php
Expand Up @@ -101,13 +101,22 @@
),
'zftool-create-controller' => array(
'options' => array(
'route' => 'create controller <name> <module>',
'route' => 'create controller <name> <module> [<path>]',
'defaults' => array(
'controller' => 'ZFTool\Controller\Create',
'action' => 'controller',
),
),
),
'zftool-create-action' => array(
'options' => array(
'route' => 'create action <name> <controllerName> <module>',
'defaults' => array(
'controller' => 'ZFTool\Controller\Create',
'action' => 'method',
),
),
),
'zftool-install-zf' => array(
'options' => array(
'route' => 'install zf <path> [<version>]',
Expand Down
2 changes: 1 addition & 1 deletion docs/DIAGNOSTICS.md
Expand Up @@ -367,7 +367,7 @@ return array(
);
````

### SteamWrapperExists
### StreamWrapperExists

Check if a given stream wrapper (or an array of names) is available. For example:

Expand Down
122 changes: 103 additions & 19 deletions src/ZFTool/Controller/CreateController.php
Expand Up @@ -10,6 +10,7 @@
use Zend\Console\ColorInterface as Color;
use Zend\Code\Generator;
use Zend\Code\Reflection;
use Zend\Filter\Word\CamelCaseToDash as CamelCaseToDashFilter;

class CreateController extends AbstractActionController
{
Expand Down Expand Up @@ -69,21 +70,21 @@ public function projectAction()
return $this->sendError("Error during the copy of the files in $path.");
}
}
if (file_exists("$path/composer.phar")) {
exec("php $path/composer.phar self-update");
if (file_exists("$path/composer.phar")) {
exec("php $path/composer.phar self-update");
} else {
if (!file_exists("$tmpDir/composer.phar")) {
if (!file_exists("$tmpDir/composer_installer.php")) {
file_put_contents(
if (!file_exists("$tmpDir/composer_installer.php")) {
file_put_contents(
"$tmpDir/composer_installer.php",
'?>' . file_get_contents('https://getcomposer.org/installer')
'?>' . file_get_contents('https://getcomposer.org/installer')
);
}
exec("php $tmpDir/composer_installer.php --install-dir $tmpDir");
}
copy("$tmpDir/composer.phar", "$path/composer.phar");
}
chmod("$path/composer.phar", 0755);
exec("php $tmpDir/composer_installer.php --install-dir $tmpDir");
}
copy("$tmpDir/composer.phar", "$path/composer.phar");
}
chmod("$path/composer.phar", 0755);
$console->writeLine("ZF2 skeleton application installed in $path.", Color::GREEN);
$console->writeLine("In order to execute the skeleton application you need to install the ZF2 library.");
$console->writeLine("Execute: \"composer.phar install\" in $path");
Expand All @@ -97,7 +98,7 @@ public function controllerAction()
$request = $this->getRequest();
$name = $request->getParam('name');
$module = $request->getParam('module');
$path = '.';
$path = $request->getParam('path', '.');

if (!file_exists("$path/module") || !file_exists("$path/config/application.config.php")) {
return $this->sendError(
Expand Down Expand Up @@ -136,9 +137,12 @@ public function controllerAction()
)
);

$dir = $path . "/module/$module/view/" . strtolower($module) . "/" . strtolower($name);
$filter = new CamelCaseToDashFilter();
$viewfolder = strtolower($filter->filter($module));

$dir = $path . "/module/$module/view/$viewfolder/" . strtolower($filter->filter($name));
if (!file_exists($dir)) {
mkdir($dir);
mkdir($dir, 0777, true);
}

$phtml = false;
Expand All @@ -154,6 +158,86 @@ public function controllerAction()
}
}

public function methodAction()
{
$console = $this->getServiceLocator()->get('console');
$request = $this->getRequest();
$action = $request->getParam('name');
$controller = $request->getParam('controllerName');
$module = $request->getParam('module');
$path = $request->getParam('path', '.');
$ucController = ucfirst($controller);
$controllerPath = sprintf('%s/module/%s/src/%s/Controller/%sController.php', $path, $module, $module, $ucController);
$class = sprintf('%s\\Controller\\%sController', $module, $ucController);


$console->writeLine("Creating action '$action' in controller '$module\\Controller\\$controller'.", Color::YELLOW);

if (!file_exists("$path/module") || !file_exists("$path/config/application.config.php")) {
return $this->sendError(
"The path $path doesn't contain a ZF2 application. I cannot create a controller action."
);
}
if (!file_exists($controllerPath)) {
return $this->sendError(
"The controller $controller does not exists in module $module. I cannot create a controller action."
);
}

$fileReflection = new Reflection\FileReflection($controllerPath, true);
$classReflection = $fileReflection->getClass($class);

$classGenerator = Generator\ClassGenerator::fromReflection($classReflection);
$classGenerator->addUse('Zend\Mvc\Controller\AbstractActionController')
->addUse('Zend\View\Model\ViewModel')
->setExtendedClass('AbstractActionController');

if ($classGenerator->hasMethod($action . 'Action')) {
return $this->sendError(
"The action $action already exists in controller $controller of module $module."
);
}

$classGenerator->addMethods(array(
new Generator\MethodGenerator(
$action . 'Action',
array(),
Generator\MethodGenerator::FLAG_PUBLIC,
'return new ViewModel();'
),
));

$fileGenerator = new Generator\FileGenerator(
array(
'classes' => array($classGenerator),
)
);

$filter = new CamelCaseToDashFilter();
$phtmlPath = sprintf(
'%s/module/%s/view/%s/%s/%s.phtml',
$path,
$module,
strtolower($filter->filter($module)),
strtolower($filter->filter($controller)),
strtolower($filter->filter($action))
);
if (!file_exists($phtmlPath)) {
$contents = sprintf("Module: %s\nController: %s\nAction: %s", $module, $controller, $action);
if (file_put_contents($phtmlPath, $contents)) {
$console->writeLine(sprintf("Created view script at %s", $phtmlPath), Color::GREEN);
} else {
$console->writeLine(sprintf("An error occurred when attempting to create view script at location %s", $phtmlPath), Color::RED);
}
}

if (file_put_contents($controllerPath, $fileGenerator->generate())) {
$console->writeLine(sprintf('The action %s has been created in controller %s\\Controller\\%s.', $action, $module, $controller), Color::GREEN);
} else {
$console->writeLine("There was an error during action creation.", Color::RED);
}
}

public function moduleAction()
{
$console = $this->getServiceLocator()->get('console');
Expand All @@ -177,13 +261,13 @@ public function moduleAction()
);
}

$filter = new CamelCaseToDashFilter();
$viewfolder = strtolower($filter->filter($name));

$name = ucfirst($name);
mkdir("$path/module/$name");
mkdir("$path/module/$name/config");
mkdir("$path/module/$name/src");
mkdir("$path/module/$name/src/$name");
mkdir("$path/module/$name/src/$name/Controller");
mkdir("$path/module/$name/view");
mkdir("$path/module/$name/config", 0777, true);
mkdir("$path/module/$name/src/$name/Controller", 0777, true);
mkdir("$path/module/$name/view/$viewfolder", 0777, true);

// Create the Module.php
file_put_contents("$path/module/$name/Module.php", Skeleton::getModule($name));
Expand Down
2 changes: 1 addition & 1 deletion src/ZFTool/Diagnostics/Exception/ExceptionInterface.php
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
2 changes: 1 addition & 1 deletion src/ZFTool/Diagnostics/Exception/RuntimeException.php
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
16 changes: 12 additions & 4 deletions src/ZFTool/Module.php
Expand Up @@ -82,14 +82,22 @@ 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>' => '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.'),
'create controller <name> <module> [<path>]' => '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'),

'Action creation:',
'create action <name> <controllerName> <module> [<path>]' => 'create an action in a controller',
array('<name>', 'The name of the action to be created'),
array('<controllerName>', 'The name of the controller in which the action should be created'),
array('<module>', 'The module containing the controller'),
array('<path>', 'The root path of a ZF2 application where to create the action'),

'Classmap generator:',
'classmap generate <directory> <classmap file> [--append|-a] [--overwrite|-w]' => '',
array('<directory>', 'The directory to scan for PHP classes (use "." to use current directory)'),
array('<classmap file>', 'File name for generated class map file or - for standard output.'.
array('<classmap file>', 'File name for generated class map file or - for standard output. '.
'If not supplied, defaults to autoload_classmap.php inside <directory>.'),
array('--append | -a', 'Append to classmap file if it exists'),
array('--overwrite | -w', 'Whether or not to overwrite existing classmap file'),
Expand Down
2 changes: 1 addition & 1 deletion tests/Bootstrap.php
Expand Up @@ -3,7 +3,7 @@
* ZFTool test suite
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend
*/
Expand Down
2 changes: 1 addition & 1 deletion zf.php
Expand Up @@ -4,7 +4,7 @@
* ZF2 command line tool
*
* @link http://github.com/zendframework/ZFTool for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
$basePath = getcwd();
Expand Down

0 comments on commit 567be92

Please sign in to comment.