function it_should_generate_code_for_the_given_metadata(Twig $view, Filesystem $fileSystem, Loader $loader) { $view->getLoader()->willReturn($loader); $this->beConstructedWith($view, $fileSystem); $formMetadata = new FormMetadata(); $formMetadata->populate('My\\Awesome\\LoginForm', ['username' => ['type' => 'text', 'optional' => false, 'multipleSelection' => false, 'value' => null], 'languages' => ['type' => 'select', 'optional' => true, 'multipleSelection' => true, 'value' => null], 'remember_me' => ['type' => 'checkbox', 'optional' => true, 'multipleSelection' => false, 'value' => null]]); $formMetadata->setTargetDirectory('src/'); $view->render('templates/class.php.twig', ['form' => $formMetadata])->willReturn($code = <<<CODE <?php namespace My\\Awesome; use EasyForms\\Elements\\Text; use EasyForms\\Elements\\Select; use EasyForms\\Elements\\Checkbox; use EasyForms\\Form; class LoginForm extends Form { public function __construct() { \$this ->add(new Text('username')) ->add((new Select('languages'))->makeOptional()->enableMultipleSelection()) ->add((new Checkbox('remember_me', 'remember'))->makeOptional()) ; } } CODE ); $this->generate($formMetadata); $fileSystem->mkdir('src/My/Awesome')->shouldHaveBeenCalled(); $fileSystem->dumpFile('src/My/Awesome/LoginForm.php', $code)->shouldHaveBeenCalled(); }
/** * @param InputInterface $input * @param OutputInterface $output * @return void */ protected function execute(InputInterface $input, OutputInterface $output) { /** @var FormHelper $formHelper */ $formHelper = $this->getHelper('form'); $output->writeln("\n<comment>Generating the code for:</comment> <info>{$input->getArgument('class')}</info>\n"); $this->metadata->populate($input->getArgument('class'), $formHelper->addElements($input, $output)); $this->metadata->setTargetDirectory($input->getArgument('directory')); $this->generator->generate($this->metadata); $formHelper->showForm($this->metadata, $output); }
/** * @param FormMetadata $formMetadata * @return string */ public function generate(FormMetadata $formMetadata) { $formMetadata->setCode($this->view->render('templates/class.php.twig', ['form' => $formMetadata])); $this->fileSystem->mkdir($formMetadata->classDirectory()); $this->fileSystem->dumpFile($formMetadata->classFilename(), $formMetadata->code()); }
/** * @param FormMetadata $metadata * @param OutputInterface $output */ public function showForm(FormMetadata $metadata, OutputInterface $output) { $output->writeln("\n<comment>{$metadata->className()} generated at:</comment>"); $output->writeln("<info>{$metadata->classFilename()}</info>"); $output->writeln("\n<comment>With code:</comment>"); $output->writeln("<info>{$metadata->code()}</info>\n"); }
* * This source file is subject to the license that is bundled with this package in the file LICENSE. * * @copyright Comunidad PHP Puebla 2015 (http://www.comunidadphppuebla.com) */ use EasyForms\Bridges\Symfony\Console\Command\GenerateFormCommand; use EasyForms\Bridges\Symfony\Console\Helper\FormHelper; use EasyForms\CodeGeneration\Forms\FormGenerator; use EasyForms\CodeGeneration\Forms\FormMetadata; use Symfony\Component\Console\Application; use Symfony\Component\Filesystem\Filesystem; use Twig_Loader_Filesystem as Loader; use Twig_Environment as Twig; $paths = [getcwd() . '/vendor/autoload.php', getcwd() . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/../../../autoload.php']; $composerAutoLoaderFound = false; foreach ($paths as $autoLoaderPath) { if (file_exists($autoLoaderPath)) { require $autoLoaderPath; $composerAutoLoaderFound = true; break; } } if (!$composerAutoLoaderFound) { fwrite(STDERR, 'You must set up the project dependencies first. Use the following commands:' . PHP_EOL . 'curl -s http://getcomposer.org/installer | php' . PHP_EOL . 'php composer.phar install' . PHP_EOL); exit(1); } $application = new Application('EasyForms Console Tool', '1.0@dev'); $metadata = new FormMetadata(); $application->getHelperSet()->set(new FormHelper($metadata->elementTypes())); $application->add(new GenerateFormCommand($metadata, new FormGenerator(new Twig(new Loader()), new Filesystem()))); $application->run();