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

Added MakeSerializerEncoder command. #30

Merged
merged 8 commits into from
Nov 28, 2017
Next Next commit
Added MakeSerializerEncoder command.
  • Loading branch information
piotrgradzinski committed Nov 19, 2017
commit e3c3d09a7e689cb64ed04981f6f0ad2bba1ea88a
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"symfony/dependency-injection": "^3.4|^4.0",
"symfony/filesystem": "^3.4|^4.0",
"symfony/framework-bundle": "^3.4|^4.0",
"symfony/http-kernel": "^3.4|^4.0"
"symfony/http-kernel": "^3.4|^4.0",
"symfony/serializer": "^3.4|^4.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in require-dev - it’s only needed to test the app.

},
"require-dev": {
"symfony/phpunit-bridge": "^3.4|^4.0",
Expand Down
75 changes: 75 additions & 0 deletions src/Command/MakeSerializerEncoderCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle\Command;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Serializer\Serializer;

/**
* @author Piotr Grabski-Gradzinski <piotr.gradzinski@gmail.com>
*/
final class MakeSerializerEncoderCommand extends AbstractCommand
{
protected static $defaultName = 'make:serializer:encoder';

protected function configure()
{
$this
->setDescription('Creates a new custom encoder class')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

Creates a new serializer encoder class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, fixed in command description, success message (as is :) ) and command help.

->addArgument('name', InputArgument::OPTIONAL, 'Choose a class name for your encoder (e.g. <fg=yellow>YamlEncoder</>).')
->addArgument('format', InputArgument::OPTIONAL, 'Pick your format name (e.g. <fg=yellow>yaml</>)')
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeSerializerEncoder.txt'))
;
}

protected function getParameters(): array
{
$encoderClassName = Str::asClassName($this->input->getArgument('name'), 'Encoder');
Validator::validateClassName($encoderClassName);
$format = $this->input->getArgument('format');

return [
'encoder_class_name' => $encoderClassName,
'format' => $format,
];
}

protected function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/serializer/Encoder.php.txt' => 'src/Serializer/'.$params['encoder_class_name'].'.php'
];
}

protected function writeNextStepsMessage(array $params, ConsoleStyle $io)
{
$io->text([
'Next: Open your new encoder class and start customizing it.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a question about your Pull Request but a general question: are these "next steps" instructions really useful?

  • This doesn't provide much: "Next: Open your new encoder class and start customizing it."
  • And this is more suited for a "demo app" or "newcomer learning resource": "Find the documentation at ..."

I propose to remove all these obvious "next steps" and only keep the ones really needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may be right. But I still want a clear “success” message at the bottom. Maybe there’s a default, generic success message and we only override it when there really are additional steps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exact next information is not really helpful in this case (just followed the convention from other commands).

Just to point that when you generate new command over SensioGeneratorBundle after confirmation you'll receive

You are going to generate a foo-bar command inside AppBundle bundle.
Do you confirm generation [yes]? 
  created ./src/AppBundle/Command/
  created ./src/AppBundle/Command/FooBarCommand.php
Generated the foo-bar command in AppBundle

                                         
  Everything is OK! Now get to work :).  

Not sure if and how close to this maker should be.

If agreed I can refactor existing commands for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK ... let's discuss about this in a separate PR and, if needed, let's make the change at once for all commands.

'Find the documentation at <fg=yellow>http://symfony.com/doc/current/serializer/custom_encoders.html</>'
]);
}

protected function configureDependencies(DependencyBuilder $dependencies)
{
$dependencies->addClassDependency(
Serializer::class,
'serializer'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use 4 space for each level of indentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thank you!

);
}
}
5 changes: 5 additions & 0 deletions src/Resources/help/MakeSerializerEncoder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The <info>%command.name%</info> command generates a new encoder class.

<info>php %command.full_name% YamlEncoder</info>

If the argument is missing, the command will ask for the class name interactively.
31 changes: 31 additions & 0 deletions src/Resources/skeleton/serializer/Encoder.php.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Serializer;

use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;

class {{ encoder_class_name }} implements EncoderInterface, DecoderInterface
{
public function encode($data, $format, array $context = array())
Copy link
Member

@dunglas dunglas Nov 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array $context = [] as we're on 7.1+?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thank you!

{
// TODO: return your encoded data
return '';
}

public function supportsEncoding($format)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just asking: we can't do it right and add type hints (e.g. supportsEncoding(string $format): bool) because of the interfaces, right?

Copy link
Contributor Author

@piotrgradzinski piotrgradzinski Nov 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiereguiluz indeed. Since there is no type-hinting in the interface, trying to add some in implemented method will end up with

Fatal error: Declaration of YamlEncoder::supportsEncoding(string $format) must be compatible with EncoderInterface::supportsEncoding($format)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact you could at least add the return type: https://3v4l.org/GiabR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ogizanagi right! Done!

{
return '{{ format }}' === $format;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ogizanagi - makes sense. Fixed.

}

public function decode($data, $format, array $context = array())
{
// TODO: return your decoded data
return '';
}

public function supportsDecoding($format)
{
return '{{ format }}' === $format;
}
}
11 changes: 11 additions & 0 deletions tests/Command/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Bundle\MakerBundle\Command\MakeEntityCommand;
use Symfony\Bundle\MakerBundle\Command\MakeFormCommand;
use Symfony\Bundle\MakerBundle\Command\MakeFunctionalTestCommand;
use Symfony\Bundle\MakerBundle\Command\MakeSerializerEncoderCommand;
use Symfony\Bundle\MakerBundle\Command\MakeSubscriberCommand;
use Symfony\Bundle\MakerBundle\Command\MakeTwigExtensionCommand;
use Symfony\Bundle\MakerBundle\Command\MakeUnitTestCommand;
Expand Down Expand Up @@ -151,6 +152,16 @@ public function getCommandTests()
],
];

$commands['serializer_encoder'] = [
new MakeSerializerEncoderCommand($generator),
[
// encoder class name
'FooBarEncoder',
// encoder format
'foobar'
]
];

$commands['twig_extension'] = [
new MakeTwigExtensionCommand($generator),
[
Expand Down