-
-
Notifications
You must be signed in to change notification settings - Fork 405
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
Changes from 1 commit
e3c3d09
df17425
f02ce64
5cfc4f8
7051ecb
74e098b
6dc2d2f
6079823
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
I propose to remove all these obvious "next steps" and only keep the ones really needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Not sure if and how close to this maker should be. If agreed I can refactor existing commands for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use 4 space for each level of indentation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. Thank you! |
||
); | ||
} | ||
} |
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. |
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ogizanagi right! Done! |
||
{ | ||
return '{{ format }}' === $format; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format could be in a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
There was a problem hiding this comment.
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.