Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] code generator #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
8 changes: 8 additions & 0 deletions scripts/generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env php
<?php
require __DIR__ . '/../vendor/autoload.php';

$console = new \Symfony\Component\Console\Application('Code Generator');
$console->add(new \MPScholten\RequestParser\Foundation\Command\GenerateParser());

$console->run();
37 changes: 37 additions & 0 deletions src/Foundation/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace MPScholten\RequestParser\Foundation\Command;

use Symfony\Component\Console\Command\Command;

abstract class AbstractCommand extends Command
{
private function getApplicationPath()
{
$path = getcwd();
if (!$this->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;
}
}
74 changes: 74 additions & 0 deletions src/Foundation/Command/GenerateParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace MPScholten\RequestParser\Foundation\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GenerateParser extends AbstractCommand
{
protected function configure()
{
$this
->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('<error>The class name has to end with `Praser`, e.g. `IpAddressParser`</error>');
return 1;
}
if (!$this->startsWithUpperCase($name)) {
$output->writeln('<error>The class name has to start with an upper case latter</error>');
return 1;
}

$path = $this->getPathTo("src/$name.php");
$content = $this->generateContent($name);

file_put_contents($path, $content);

$output->writeln("<info> + $path</info>");
}

private function generateContent($name)
{
return <<<CODE
<?php

namespace MPScholten\RequestParser;

class $name extends AbstractValueParser
{
protected function describe()
{
// TODO: describe the expected input, e.g. "a valid IP address"
return "a ...";
}

protected function parse(\$value)
{
// TODO: handle type casting, validation, etc.
return (string) \$value;
}

public function defaultsTo(\$defaultValue)
{
return parent::defaultsTo(\$defaultValue);
}

public function required(\$invalidValueMessage = null, \$notFoundMessage = null)
{
return parent::required(\$invalidValueMessage, \$notFoundMessage);
}
}

CODE;

}
}