-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from EdouardTack/shell
Add command shell to create Action File
- Loading branch information
Showing
15 changed files
with
752 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
- | ||
class: HavokInspiration\ActionsClass\PHPStan\ShellPropertiesClassReflectionExtension | ||
tags: | ||
- phpstan.broker.propertiesClassReflectionExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
<?php | ||
/** | ||
* Copyright (c) Yves Piquel (http://www.havokinspiration.fr) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Yves Piquel (http://www.havokinspiration.fr) | ||
* @link http://github.com/HavokInspiration/cakephp-actions-class | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
declare(strict_types=1); | ||
namespace HavokInspiration\ActionsClass\Shell\Task; | ||
|
||
use Cake\Console\Shell; | ||
use Bake\Shell\Task\SimpleBakeTask; | ||
use Cake\Core\Configure; | ||
|
||
/** | ||
* Command line to create HavokInspiration\ActionsClass action file | ||
*/ | ||
class ActionTask extends SimpleBakeTask | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public $pathFragment = 'Controller/'; | ||
|
||
/** | ||
* Tasks to be loaded by this Task | ||
* | ||
* @var array | ||
*/ | ||
public $tasks = [ | ||
'Bake.BakeTemplate', | ||
'Bake.Test' | ||
]; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function name() | ||
{ | ||
return 'action'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function fileName($name, $test = false) | ||
{ | ||
$fileName = $name . 'Action'; | ||
if ($test) { | ||
$fileName .= 'Test'; | ||
} | ||
$fileName .= '.php'; | ||
|
||
return $fileName; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function template() | ||
{ | ||
return 'HavokInspiration/ActionsClass.action'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function templateTest() | ||
{ | ||
return 'HavokInspiration/ActionsClass.test'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function bake($name) | ||
{ | ||
if (strpos($name, '/') === false) { | ||
$this->err('You must pass a Controller name for your action in the format `ControllerName/ActionName`'); | ||
|
||
return (string)Shell::CODE_ERROR; | ||
} | ||
|
||
$this->out("\n" . sprintf('Baking action class for %s...', $name), 1, Shell::QUIET); | ||
|
||
list($controller, $action) = $this->getName($name); | ||
|
||
$namespace = Configure::read('App.namespace'); | ||
if ($this->plugin) { | ||
$namespace = $this->_pluginNamespace($this->plugin); | ||
} | ||
|
||
$prefix = $this->_getPrefix(); | ||
if ($prefix) { | ||
$prefix = '\\' . str_replace('/', '\\', $prefix); | ||
} | ||
|
||
$data = [ | ||
'action' => $action, | ||
'controller' => $controller, | ||
'namespace' => $namespace, | ||
'prefix' => $prefix | ||
]; | ||
|
||
$out = $this->bakeAction($action, $data); | ||
|
||
if (!isset($this->params['no-test']) || $this->params['no-test'] !== true) { | ||
$this->bakeActionTest($action, $data); | ||
} | ||
|
||
return $out; | ||
} | ||
|
||
/** | ||
* Generate the action code | ||
* | ||
* @param string $actionName The name of the action. | ||
* @param array $data The data to turn into code. | ||
* @return string The generated action file. | ||
*/ | ||
public function bakeAction($actionName, array $data) | ||
{ | ||
$data += [ | ||
'namespace' => null, | ||
'controller' => null, | ||
'prefix' => null, | ||
'actions' => null, | ||
]; | ||
$this->BakeTemplate->set($data); | ||
$contents = $this->BakeTemplate->generate($this->template()); | ||
$path = $this->getPath(); | ||
$filename = $path . $data['controller'] . DS . $this->fileName($actionName); | ||
$this->createFile($filename, $contents); | ||
|
||
return $contents; | ||
} | ||
|
||
/** | ||
* Assembles and writes a unit test file | ||
* | ||
* @param string $className Controller class name | ||
* @return string|null Baked test | ||
*/ | ||
public function bakeActionTest($actionName, $data) | ||
{ | ||
$data += [ | ||
'namespace' => null, | ||
'controller' => null, | ||
'prefix' => null, | ||
'actions' => null, | ||
]; | ||
$this->Test->plugin = $this->plugin; | ||
$this->BakeTemplate->set($data); | ||
|
||
$prefix = $this->_getPrefix(); | ||
$contents = $this->BakeTemplate->generate($this->templateTest()); | ||
|
||
$path = $this->Test->getPath() . 'Controller' . DS; | ||
if ($prefix) { | ||
$path .= $prefix . DS; | ||
} | ||
$path .= $data['controller']; | ||
|
||
$filename = $path . DS . $this->fileName($actionName, true); | ||
$this->createFile($filename, $contents); | ||
|
||
return $contents; | ||
} | ||
|
||
/** | ||
* Transform the name parameter into Controller & Action name. | ||
* | ||
* @param string $name Name passed to the CLI. | ||
* @return array First key is the controller name, second key the action name. | ||
*/ | ||
protected function getName($name) | ||
{ | ||
list($controller, $action) = explode('/', $name); | ||
|
||
$controller = $this->_camelize($controller); | ||
$action = $this->_camelize($action); | ||
|
||
return [$controller, $action]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getOptionParser() | ||
{ | ||
$parser = parent::getOptionParser(); | ||
|
||
$parser | ||
->setDescription( | ||
'Bake an Action class file skeleton' | ||
) | ||
->addOption('prefix', [ | ||
'help' => 'The namespace/routing prefix to use.' | ||
]) | ||
->addOption('no-test', [ | ||
'boolean' => true, | ||
'help' => 'Do not generate a test skeleton.' | ||
]); | ||
|
||
return $parser; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
namespace <%= $namespace %>\Controller<%= $prefix %>\<%= $controller %>; | ||
|
||
use HavokInspiration\ActionsClass\Controller\Action; | ||
|
||
/** | ||
* Controller : <%= $controller %> | ||
* Action : <%= $action %> | ||
* | ||
* @package <%= $namespace %>\Controller | ||
*/ | ||
class <%= $action %>Action extends Action | ||
{ | ||
/** | ||
* This method will be executed when the `<%= $controller %>` Controller action `<%= $action %>` will be invoked. | ||
* It is the equivalent of the `<%= $controller %>Controller::<%= $action %>()` method. | ||
* | ||
* @return void|\Cake\Network\Response | ||
*/ | ||
public function execute() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
namespace <%= $namespace %>\Test\TestCase\Controller<%= $prefix %>\<%= $controller %>; | ||
|
||
use Cake\TestSuite\IntegrationTestCase; | ||
|
||
/** | ||
* Controller <%= $controller %> | ||
* Action <%= $action %> | ||
* | ||
* @package <%= $namespace %>\Controller | ||
*/ | ||
class <%= $action %>ActionTest extends IntegrationTestCase | ||
{ | ||
/** | ||
* TestCase for \<%= $namespace %>\Controller\<%= $controller %>\<%= $action %>Action | ||
*/ | ||
public function test<%= $action %>Action() | ||
{ | ||
$this->get('<%= str_replace('\\', '/', strtolower($prefix)) %>/<%= strtolower($controller) %>/<%= strtolower($action) %>'); | ||
$this->assertResponseOk(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
namespace HavokInspiration\ActionsClass\PHPStan; | ||
|
||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\PropertyReflection; | ||
use PHPStan\Type\Type; | ||
|
||
class BakeTemplatePropertyReflection implements PropertyReflection | ||
{ | ||
/** @var \PHPStan\Reflection\ClassReflection */ | ||
private $declaringClass; | ||
|
||
/** @var \PHPStan\Type\Type */ | ||
private $type; | ||
|
||
public function __construct( | ||
ClassReflection $declaringClass, | ||
Type $type | ||
) { | ||
$this->declaringClass = $declaringClass; | ||
$this->type = $type; | ||
} | ||
public function getDeclaringClass(): ClassReflection | ||
{ | ||
return $this->declaringClass; | ||
} | ||
|
||
public function isStatic(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isPrivate(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isPublic(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getType(): Type | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function isReadable(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function isWritable(): bool | ||
{ | ||
return true; | ||
} | ||
} |
Oops, something went wrong.