Skip to content

Instantly share code, notes, and snippets.

@geerteltink
Created June 23, 2017 05:55
Show Gist options
  • Save geerteltink/9a2515a72fa8af717b6ff2acaabd7adc to your computer and use it in GitHub Desktop.
Save geerteltink/9a2515a72fa8af717b6ff2acaabd7adc to your computer and use it in GitHub Desktop.
zend-expressive-tooling command examples
<?php
declare(strict_types=1);
namespace App\Application\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Zend\Expressive\Tooling\CreateMiddleware\CreateMiddleware;
class CreateMiddlewareActionCommand extends Command
{
const CLASS_SKELETON = <<< 'EOS'
<?php
declare(strict_types=1);
namespace %namespace%;
use App\Infrastructure\View\StreamRenderer;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Expressive\Helper\UrlHelper;
class %class% implements MiddlewareInterface
{
/**
* @var StreamRenderer
*/
private $renderer;
/**
* @var UrlHelper
*/
private $urlHelper;
public function __construct(StreamRenderer $renderer, UrlHelper $urlHelper)
{
$this->renderer = $renderer;
$this->urlHelper = $urlHelper;
}
public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface
{
return new HtmlResponse($this->renderer->render($request, 'NAMESPACE::TEMPLATE'));
}
}
EOS;
const HELP = <<< 'EOT'
Creates an http-interop middleware action class file named after the provided
class. For a path, it matches the class namespace against PSR-4 autoloader
namespaces in your composer.json.
EOT;
const HELP_ARG_MIDDLEWARE = <<< 'EOT'
Fully qualified class name of the middleware to create. This value
should be quoted to ensure namespace separators are not interpreted as
escape sequences by your shell.
EOT;
/**
* Configure the console command.
*/
protected function configure()
{
$this->setName('middleware:create:action');
$this->setDescription('Create an http-interop middleware action class file.');
$this->setHelp(self::HELP);
$this->addArgument('middleware', InputArgument::REQUIRED, self::HELP_ARG_MIDDLEWARE);
}
/**
* Execute console command.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int Exit status
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$middleware = $input->getArgument('middleware');
$output->writeln(sprintf('<info>Creating middleware %s...</info>', $middleware));
$generator = new CreateMiddleware();
$path = $generator->process($middleware, null, self::CLASS_SKELETON);
$output->writeln('<info>Success!</info>');
$output->writeln(sprintf(
'<info>- Created class %s, in file %s</info>',
$middleware,
$path
));
return 0;
}
}
<?php
declare(strict_types=1);
namespace App\Application\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Zend\Expressive\Tooling\CreateMiddleware\CreateMiddleware;
class CreateMiddlewareApiCommand extends Command
{
const CLASS_SKELETON = <<< 'EOS'
<?php
declare(strict_types=1);
namespace %namespace%;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class %class% implements MiddlewareInterface
{
public function __construct()
{
}
public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface
{
return new JsonResponse([]);
}
}
EOS;
const HELP = <<< 'EOT'
Creates an http-interop middleware api class file named after the provided
class. For a path, it matches the class namespace against PSR-4 autoloader
namespaces in your composer.json.
EOT;
const HELP_ARG_MIDDLEWARE = <<< 'EOT'
Fully qualified class name of the middleware to create. This value
should be quoted to ensure namespace separators are not interpreted as
escape sequences by your shell.
EOT;
/**
* Configure the console command.
*/
protected function configure()
{
$this->setName('middleware:create:api');
$this->setDescription('Create an http-interop middleware api class file.');
$this->setHelp(self::HELP);
$this->addArgument('middleware', InputArgument::REQUIRED, self::HELP_ARG_MIDDLEWARE);
}
/**
* Execute console command.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int Exit status
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$middleware = $input->getArgument('middleware');
$output->writeln(sprintf('<info>Creating middleware %s...</info>', $middleware));
$generator = new CreateMiddleware();
$path = $generator->process($middleware, null, self::CLASS_SKELETON);
$output->writeln('<info>Success!</info>');
$output->writeln(sprintf(
'<info>- Created class %s, in file %s</info>',
$middleware,
$path
));
return 0;
}
}
<?php
declare(strict_types=1);
namespace App\Application\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Zend\Expressive\Tooling\CreateMiddleware\CreateMiddleware;
class CreateMiddlewareCommand extends Command
{
const CLASS_SKELETON = <<< 'EOS'
<?php
declare(strict_types=1);
namespace %namespace%;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class %class% implements MiddlewareInterface
{
public function __construct()
{
}
public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface
{
// Do something (with the request) first
// Call the next middleware and wait for the response
$response = $delegate->process($request);
// Do something (with the response) before returning the response
// Return the response
return $response;
}
}
EOS;
const HELP = <<< 'EOT'
Creates an http-interop middleware class file named after the provided
class. For a path, it matches the class namespace against PSR-4 autoloader
namespaces in your composer.json.
EOT;
const HELP_ARG_MIDDLEWARE = <<< 'EOT'
Fully qualified class name of the middleware to create. This value
should be quoted to ensure namespace separators are not interpreted as
escape sequences by your shell.
EOT;
/**
* Configure the console command.
*/
protected function configure()
{
$this->setName('middleware:create');
$this->setDescription('Create an http-interop middleware class file.');
$this->setHelp(self::HELP);
$this->addArgument('middleware', InputArgument::REQUIRED, self::HELP_ARG_MIDDLEWARE);
}
/**
* Execute console command.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int Exit status
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$middleware = $input->getArgument('middleware');
$output->writeln(sprintf('<info>Creating middleware %s...</info>', $middleware));
$generator = new CreateMiddleware();
$path = $generator->process($middleware, null, self::CLASS_SKELETON);
$output->writeln('<info>Success!</info>');
$output->writeln(sprintf(
'<info>- Created class %s, in file %s</info>',
$middleware,
$path
));
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment