Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/create-middleware'
Browse files Browse the repository at this point in the history
Close #22
  • Loading branch information
weierophinney committed Apr 6, 2017
2 parents 6e00af3 + f5112c1 commit 0316089
Show file tree
Hide file tree
Showing 19 changed files with 1,461 additions and 1 deletion.
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.4.0 - TBD

### Added

- [#22](https://github.com/zendframework/zend-expressive-tooling/pull/22) adds
the command `expressive-create-middleware`, which allows creating an
http-interop middleware class file. The command expects a fully-qualified
class name, and will match the namespace against existing PSR-4 namespaces in
your `composer.json` in order to determine where to create the class file.

- [#22](https://github.com/zendframework/zend-expressive-tooling/pull/22) adds
the metacommand `expressive`, which allows executing any of the other commands
provided in the package, minus their `expressive-` prefix. The command also
provides the ability to ask for individual command help using the syntax
`expressive help <command>` or `expressive <command> help`.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 0.3.2 - 2017-03-13

### Added
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ $ composer require --dev zendframework/zend-expressive-tooling

## Tools

- `vendor/bin/expressive`: Meta-command for invoking all other commands. When
using this command, you can call any of the other commands minus the
`expressive-` prefix: e.g., `expressive module create Foo`. This command also
supports `help` operations of either the form `expressive help <command>` or
`expressive <command> help`.

- `vendor/bin/expressive-create-middleware`: Create an http-interop middleware
class name; the class file is created based on matching a PSR-4 autoloader
defined in your `composer.json`.

- `vendor/bin/expressive-migrate-original-messages`: Ensure your application
does not use the Stratigility-specific PSR-7 message decorators.

Expand Down
27 changes: 27 additions & 0 deletions bin/expressive
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);
27 changes: 27 additions & 0 deletions bin/expressive-create-middleware
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);
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
}
},
"bin": [
"bin/expressive",
"bin/expressive-create-middleware",
"bin/expressive-migrate-original-messages",
"bin/expressive-module",
"bin/expressive-pipeline-from-config",
Expand Down
118 changes: 118 additions & 0 deletions src/CreateMiddleware/Command.php
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);
});
}
}
Loading

0 comments on commit 0316089

Please sign in to comment.