diff --git a/composer.json b/composer.json index 438e93c..5abed73 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "phpunit/phpunit": "3.7.*", "symfony/http-foundation": "3.1.*", "symfony/psr-http-message-bridge": "^0.2.0", + "symfony/console": "3.1.*", "zendframework/zend-diactoros": "^1.3" }, "autoload": { diff --git a/scripts/generate b/scripts/generate new file mode 100755 index 0000000..8cc29f9 --- /dev/null +++ b/scripts/generate @@ -0,0 +1,8 @@ +#!/usr/bin/env php +add(new \MPScholten\RequestParser\Foundation\Command\GenerateParser()); + +$console->run(); diff --git a/src/Foundation/Command/AbstractCommand.php b/src/Foundation/Command/AbstractCommand.php new file mode 100644 index 0000000..58e3bdd --- /dev/null +++ b/src/Foundation/Command/AbstractCommand.php @@ -0,0 +1,37 @@ +isValidApplicationPath($path)) { + throw new \RuntimeException("This command has to be called from the root request-parser directory, but was invoked from $path"); + } + + return $path; + } + + protected function getPathTo($directory) + { + return $this->getApplicationPath() . "/$directory"; + } + + private function isValidApplicationPath($path) + { + return $this->endsWith($path, 'request-parser'); + } + + protected function endsWith($haystack, $needle) { + return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false); + } + + protected function startsWithUpperCase($str) { + $chr = mb_substr ($str, 0, 1, "UTF-8"); + return mb_strtolower($chr, "UTF-8") != $chr; + } +} diff --git a/src/Foundation/Command/GenerateParser.php b/src/Foundation/Command/GenerateParser.php new file mode 100644 index 0000000..0471cb3 --- /dev/null +++ b/src/Foundation/Command/GenerateParser.php @@ -0,0 +1,74 @@ +setName('parser') + ->setDescription('Generates an empty custom parser class') + ->addArgument('name', InputArgument::REQUIRED, 'The parser name, e.g. `IpAddressParser`'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $name = $input->getArgument('name'); + if (!$this->endsWith($name, 'Parser')) { + $output->writeln('The class name has to end with `Praser`, e.g. `IpAddressParser`'); + return 1; + } + if (!$this->startsWithUpperCase($name)) { + $output->writeln('The class name has to start with an upper case latter'); + return 1; + } + + $path = $this->getPathTo("src/$name.php"); + $content = $this->generateContent($name); + + file_put_contents($path, $content); + + $output->writeln(" + $path"); + } + + private function generateContent($name) + { + return <<