This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/create-middleware'
Close #22
- Loading branch information
Showing
19 changed files
with
1,461 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env php | ||
<?php // @codingStandardsIgnoreFile | ||
/** | ||
* Wrapper for all other commands | ||
* | ||
* @see https://github.com/zendframework/zend-expressive-tooling for the canonical source repository | ||
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive-tooling/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Expressive\Tooling; | ||
|
||
use Zend\Stdlib\ConsoleHelper; | ||
|
||
// Setup/verify autoloading | ||
if (file_exists($a = __DIR__ . '/../../../autoload.php')) { | ||
require $a; | ||
} elseif (file_exists($a = __DIR__ . '/../vendor/autoload.php')) { | ||
require $a; | ||
} else { | ||
fwrite(STDERR, 'Cannot locate autoloader; please run "composer install"' . PHP_EOL); | ||
exit(1); | ||
} | ||
|
||
$command = new ExpressiveCommand($argv[0], new ConsoleHelper()); | ||
$return = $command->process(array_slice($argv, 1)); | ||
exit($return); |
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,27 @@ | ||
#!/usr/bin/env php | ||
<?php // @codingStandardsIgnoreFile | ||
/** | ||
* Script for creating PSR-15 middleware in a project | ||
* | ||
* @see https://github.com/zendframework/zend-expressive-tooling for the canonical source repository | ||
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive-tooling/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Expressive\Tooling\CreateMiddleware; | ||
|
||
use Zend\Stdlib\ConsoleHelper; | ||
|
||
// Setup/verify autoloading | ||
if (file_exists($a = __DIR__ . '/../../../autoload.php')) { | ||
require $a; | ||
} elseif (file_exists($a = __DIR__ . '/../vendor/autoload.php')) { | ||
require $a; | ||
} else { | ||
fwrite(STDERR, 'Cannot locate autoloader; please run "composer install"' . PHP_EOL); | ||
exit(1); | ||
} | ||
|
||
$command = new Command($argv[0], new ConsoleHelper()); | ||
$return = $command->process(array_slice($argv, 1)); | ||
exit($return); |
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,118 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-expressive-tooling for the canonical source repository | ||
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive-tooling/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Expressive\Tooling\CreateMiddleware; | ||
|
||
use Zend\Stdlib\ConsoleHelper; | ||
|
||
class Command | ||
{ | ||
const DEFAULT_COMMAND_NAME = 'expressive-create-middleware'; | ||
|
||
public $projectDir = '.'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $command; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $helpArgs = ['--help', '-h', 'help']; | ||
|
||
/** | ||
* @var ConsoleHelper | ||
*/ | ||
private $console; | ||
|
||
/** | ||
* @param string $command Script that is invoking the command. | ||
* @param null|ConsoleHelper $console | ||
*/ | ||
public function __construct($command = self::DEFAULT_COMMAND_NAME, ConsoleHelper $console = null) | ||
{ | ||
$this->command = (string) $command; | ||
$this->console = $console ?: new ConsoleHelper(); | ||
} | ||
|
||
/** | ||
* @param array $args | ||
* @return int | ||
*/ | ||
public function process(array $args) | ||
{ | ||
if ($this->isHelpRequest($args)) { | ||
$help = new Help($this->command, $this->console); | ||
$help(STDOUT); | ||
return 0; | ||
} | ||
|
||
$args = $this->filterArgs($args); | ||
$middleware = array_shift($args); | ||
|
||
$this->console->writeLine(sprintf('<info>Creating middleware %s...</info>', $middleware)); | ||
|
||
$generator = new CreateMiddleware(); | ||
|
||
try { | ||
$path = $generator->process($middleware, $this->projectDir); | ||
} catch (CreateMiddlewareException $e) { | ||
$this->console->writeLine('<error>Error during generation:</error>', true, STDERR); | ||
$this->console->writeLine(sprintf(' <error>%s</error>', $e->getMessage()), true, STDERR); | ||
return 1; | ||
} | ||
|
||
$this->console->writeLine('<info>Success!</info>'); | ||
$this->console->writeLine(sprintf( | ||
'<info>- Created class %s, in file %s</info>', | ||
$middleware, | ||
$path | ||
)); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Is this a help request? | ||
* | ||
* @param array $args | ||
* @return bool | ||
*/ | ||
private function isHelpRequest(array $args) | ||
{ | ||
$numArgs = count($args); | ||
if (0 === $numArgs) { | ||
return true; | ||
} | ||
|
||
$arg = array_shift($args); | ||
|
||
if (in_array($arg, $this->helpArgs, true)) { | ||
return true; | ||
} | ||
|
||
if (empty($args)) { | ||
return false; | ||
} | ||
|
||
$arg = array_shift($args); | ||
|
||
return in_array($arg, $this->helpArgs, true); | ||
} | ||
|
||
/** | ||
* @param array $args | ||
* @return array | ||
*/ | ||
private function filterArgs(array $args) | ||
{ | ||
return array_filter($args, function ($arg) { | ||
return ! in_array($arg, $this->helpArgs, true); | ||
}); | ||
} | ||
} |
Oops, something went wrong.