-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 'create action class from verb and path' functionality
- Loading branch information
Paul M. Jones
committed
Dec 30, 2019
1 parent
494658a
commit fdae3ca
Showing
7 changed files
with
287 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
use AutoRoute\AutoRoute; | ||
|
||
$autoload = require dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
$options = getopt('', ['method:', 'suffix:', 'template:', 'word-separator:'], $optind); | ||
|
||
$namespace = $argv[$optind + 0] ?? null; | ||
if ($namespace === null) { | ||
echo "Please pass a PHP namespace as the first argument." . PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$directory = realpath($argv[$optind + 1] ?? null); | ||
if ($directory === false) { | ||
echo "Please pass the PHP namespace directory path as the second argument." . PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$verb = $argv[$optind + 2] ?? null; | ||
if ($verb === null) { | ||
echo "Please pass an HTTP verb as the third argument." . PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$path = $argv[$optind + 3] ?? null; | ||
if ($path === null) { | ||
echo "Please pass a URL path as the fourth argument." . PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$template = $options['template'] ?? dirname(__DIR__) . '/resources/templates/action.tpl'; | ||
if (! file_exists($template)) { | ||
echo "Template file {$template} does not exist." . PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
// --- | ||
|
||
$autoRoute = (new AutoRoute($namespace, $directory)) | ||
->setMethod($options['method'] ?? '__invoke') | ||
->setSuffix($options['suffix'] ?? '') | ||
->setWordSeparator($options['word-separator'] ?? '-'); | ||
|
||
$creator = $autoRoute->newCreator(file_get_contents($template)); | ||
|
||
[$file, $code] = $creator->create($verb, $path); | ||
|
||
echo $file . PHP_EOL; | ||
if (file_exists($file)) { | ||
echo "Already exists; not overwriting." . PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$dir = dirname($file); | ||
if (! is_dir($dir)) { | ||
mkdir($dir, 0777, true); | ||
} | ||
|
||
file_put_contents($file, $code); | ||
|
||
exit(0); |
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,9 @@ | ||
<?php | ||
namespace {NAMESPACE}; | ||
|
||
class {CLASS} | ||
{ | ||
public function {METHOD}({PARAMETERS}) | ||
{ | ||
} | ||
} |
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,98 @@ | ||
<?php | ||
/** | ||
* | ||
* This file is part of AutoRoute for PHP. | ||
* | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace AutoRoute; | ||
|
||
class Creator | ||
{ | ||
protected $namespace; | ||
|
||
protected $directory; | ||
|
||
protected $suffix; | ||
|
||
protected $method; | ||
|
||
protected $wordSeparator; | ||
|
||
protected $template; | ||
|
||
public function __construct( | ||
string $namespace, | ||
string $directory, | ||
string $suffix, | ||
string $method, | ||
string $wordSeparator, | ||
string $template | ||
) { | ||
$this->namespace = trim($namespace, '\\') . '\\'; | ||
$this->directory = rtrim($directory, DIRECTORY_SEPARATOR); | ||
$this->suffix = $suffix; | ||
$this->method = $method; | ||
$this->wordSeparator = $wordSeparator; | ||
$this->template = $template; | ||
} | ||
|
||
public function create(string $verb, string $path) : array | ||
{ | ||
[$namespace, $directory, $class, $parameters] = $this->parse($verb, $path); | ||
$file = "{$directory}/{$class}.php"; | ||
$vars = [ | ||
'{NAMESPACE}' => $namespace, | ||
'{CLASS}' => $class, | ||
'{METHOD}' => $this->method, | ||
'{PARAMETERS}' => $parameters, | ||
]; | ||
$code = strtr($this->template, $vars); | ||
return [$file, $code]; | ||
} | ||
|
||
protected function parse(string $verb, string $path) : array | ||
{ | ||
$segments = explode('/', trim($path, '/')); | ||
$subNamespaces = []; | ||
$parameters = []; | ||
|
||
while (! empty($segments)) { | ||
$segment = array_shift($segments); | ||
|
||
if (substr($segment, 0, 1) == '{') { | ||
$parameters[] = '$' . trim($segment, '{} '); | ||
continue; | ||
} | ||
|
||
$segment = str_replace($this->wordSeparator, ' ', $segment); | ||
$segment = str_replace(' ', '', ucwords($segment)); | ||
$subNamespaces[] = $segment; | ||
} | ||
|
||
$namespace = ''; | ||
|
||
foreach ($subNamespaces as $subNamespace) { | ||
$namespace .= ucfirst(strtolower($subNamespace)) . '\\'; | ||
} | ||
|
||
$namespace = rtrim($namespace, '\\'); | ||
|
||
$class = ucfirst(strtolower($verb)) | ||
. str_replace('\\', '', $namespace) | ||
. $this->suffix; | ||
|
||
$directory = $this->directory . DIRECTORY_SEPARATOR | ||
. str_replace('\\', DIRECTORY_SEPARATOR, $namespace); | ||
|
||
return [ | ||
rtrim($this->namespace . $namespace, '\\'), | ||
$directory, | ||
$class, | ||
implode(', ', $parameters) | ||
]; | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace AutoRoute; | ||
|
||
class CreatorTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$autoRoute = new AutoRoute( | ||
'AutoRoute\\Http', | ||
__DIR__ . DIRECTORY_SEPARATOR . 'Http' | ||
); | ||
$autoRoute->setSuffix('Action'); | ||
$autoRoute->setMethod('exec'); | ||
$creator = $autoRoute->newCreator(file_get_contents( | ||
dirname(__DIR__) . '/resources/templates/action.tpl' | ||
)); | ||
|
||
[$file, $code] = $creator->create( | ||
'GET', | ||
'/company/{companyId}/employee/{employeeNum}' | ||
); | ||
|
||
$expect = str_replace( | ||
'/', | ||
DIRECTORY_SEPARATOR, | ||
__DIR__ . DIRECTORY_SEPARATOR . 'Http/Company/Employee/GetCompanyEmployeeAction.php' | ||
); | ||
|
||
$this->assertSame($expect, $file); | ||
|
||
$expect = '<?php | ||
namespace AutoRoute\Http\Company\Employee; | ||
class GetCompanyEmployeeAction | ||
{ | ||
public function exec($companyId, $employeeNum) | ||
{ | ||
} | ||
} | ||
'; | ||
|
||
$this->assertSame($expect, $code); | ||
} | ||
} |