Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function execute(ArgumentList $args, BuilderInterface $builder, $simulate)
 {
     $dir = $builder->target($args[0]);
     $mode = $args->get(1, 0777);
     if (file_exists($dir)) {
         throw new ActionFailedException("Target directory {$args[0]} already exists");
     }
     if (!$simulate) {
         mkdir($dir, $mode, $args->get(2, true));
     }
     $builder->did('mkdir', $args[0], [decoct($mode)]);
 }
 /**
  * {@inheritdoc}
  */
 public function execute(ArgumentList $args, BuilderInterface $builder, $simulate)
 {
     $file = $builder->target($args[0]);
     $mode = $args->get(1, 0644);
     if (file_exists($file)) {
         throw new ActionFailedException("Cannot create {$file}, file already exists.");
     }
     if (!$simulate) {
         touch($file);
         chmod($file, $mode);
     }
     $builder->did('create', $args[0], [decoct($mode)]);
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public function execute(ArgumentList $args, BuilderInterface $builder, $simulate)
 {
     $file = $builder->target($args[0]);
     $mode = $args->get(1, 0644);
     if (!file_exists($file)) {
         $builder->exec('create', [$file, $mode]);
     } else {
         if (!$simulate) {
             touch($file);
         }
         $builder->did('touch', $args[0]);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function execute(ArgumentList $args, BuilderInterface $builder, $simulate)
 {
     $file = $builder->target($args[0]);
     $modifier = $args[1];
     if (!is_callable($modifier)) {
         throw new ActionFailedException("Modifier parameter should be a callable");
     }
     if (!file_exists($file)) {
         throw new ActionFailedException("File {$file} does not exist, cannot modify");
     }
     $contents = file_get_contents($file);
     $modified = $modifier($contents);
     if (!$simulate && is_string($modified)) {
         file_put_contents($file, $modified);
     }
     $builder->did('modify', $args[0]);
 }
 /**
  * {@inheritdoc}
  */
 public function execute(ArgumentList $args, BuilderInterface $builder, $simulate)
 {
     $file = $builder->target($args[0]);
     $from = $args[1];
     $to = $args[2];
     $regex = $args->get(3, false);
     $limit = $args->get(4, -1);
     $offset = $args->get(5, 0);
     if (!file_exists($file) || !is_readable($file)) {
         throw new ActionFailedException("File {$file} does not exist or is not readable.");
     }
     $content = file_get_contents($file);
     if ($regex) {
         $content = StringUtil::regexReplace($from, $to, $content, $offset, $limit);
     } else {
         $content = StringUtil::stringReplace($from, $to, $content, $offset, $limit);
     }
     if (!$simulate) {
         file_put_contents($file, $content);
     }
     $builder->did('replace', $args[0], [$from, $to]);
 }
 /**
  * {@inheritdoc}
  */
 public function execute(ArgumentList $args, BuilderInterface $builder, $simulate)
 {
     $file = $builder->target($args[0]);
     $params = $args->get(2, false);
     if (is_array($params) || $params === true) {
         /** @var TemplateAction $templateAction */
         $templateAction = $builder->getAction('template');
         $template = $builder->source($args[1]);
         $string = $templateAction->getTemplateContent($template, is_array($params) ? $params : []);
     } else {
         $string = $args[1];
     }
     if (!file_exists($file)) {
         throw new ActionFailedException("Cannot append to non-existing file {$file}");
     }
     $fp = fopen($file, 'a');
     if (!$simulate) {
         fwrite($fp, $string);
     }
     fclose($fp);
     $builder->did('append', $args[0]);
 }
 /**
  * {@inheritdoc}
  */
 public function dispatch($generator, $arguments, $path = null, $simulate = false)
 {
     if (is_string($generator)) {
         $generator = $this->registry->getGenerator($generator);
     }
     if (is_array($arguments)) {
         $arguments = new Arguments($arguments);
     }
     $generator->before($arguments);
     $constraints = $generator->getConstraints();
     // retrieve interactive input
     if ($arguments->isForcedInteractive()) {
         $questioner = new Questioner($this->getInputTypeRegistry(), $this->getOutput(), $this->getHelperSet(), $constraints, $this->validator);
         $generator->beforeInteract($arguments);
         $generator->interact($arguments, $questioner);
     }
     // run validation
     $generator->beforeValidate($arguments);
     $constraints = new Collection($constraints);
     $constraints->allowExtraFields = true;
     $problems = $this->validator->validateValue($arguments->getData(), $constraints);
     // stop if any problems are found
     if (count($problems) > 0) {
         $this->showValidationProblems($problems, $generator);
         throw new ValidationException("Generator failed because of validation errors");
     }
     // create a builder for this specific generator
     $generator->beforeGenerate($arguments);
     $builder = $this->builder->forGenerator($generator, $arguments);
     if ($path !== null) {
         $builder = $builder->withPath($path);
     }
     $builder->simulated($simulate);
     // run the generator
     $generator->setOutput($this->output);
     $generator->generate($arguments, $builder, $this);
 }
 /**
  * {@inheritdoc}
  */
 public function execute(ArgumentList $args, BuilderInterface $builder, $simulate)
 {
     $source = $builder->source($args[0]);
     $target = $builder->target($args[1]);
     $variables = $args->get(2, array());
     $mode = $args->get(3, 0644);
     $directory = dirname($target);
     if (!is_dir($directory)) {
         $builder->exec('mkdir', [$directory, 0755, true]);
     }
     if (file_exists($target)) {
         throw new ActionFailedException("Target {$target} already exists");
     }
     $variables['args'] = $builder->getArguments();
     $content = $this->getTemplateContent($source, $variables);
     if (!$simulate) {
         file_put_contents($target, $content);
         chmod($target, $mode);
     }
     $builder->did('generate', $args[1], [decoct($mode)]);
 }
 /**
  * {@inheritdoc}
  */
 public function generate(Arguments $arguments, BuilderInterface $builder, GeneratorDispatcherInterface $dispatcher)
 {
     $builder->mkdir($arguments['dir']);
     $builder->in($arguments['dir'], function (BuilderInterface $builder) use($arguments) {
         $builder->template('Bundle.php.twig', "{$arguments->name}.php");
         $builder->in('DependencyInjection', function (BuilderInterface $builder) use($arguments) {
             $builder->template('Configuration.php.twig', 'Configuration.php');
             $builder->template('Extension.php.twig', "{$arguments['basename']}Extension.php");
         });
         $builder->in('Resources/config', function (BuilderInterface $builder) use($arguments) {
             $builder->template('services.yml.twig', 'services.yml');
         });
         $builder->mkdir('Controller');
         $builder->mkdir('Resources/views');
         if ($arguments['structure']) {
             $builder->mkdir('Resources/doc');
             $builder->mkdir('Resources/translations');
             $builder->touch('Resources/translations/messages.nl.po');
             $builder->mkdir('Entity');
             $builder->mkdir('Form');
         }
     });
     if ($arguments['update-kernel']) {
         $bundleClass = "{$arguments['namespace']}\\{$arguments['name']}";
         $builder->modify('app/AppKernel.php', function ($content) use($bundleClass) {
             $count = preg_match("#([\t ]+)// project bundles\n#", $content, $matches, PREG_OFFSET_CAPTURE);
             if ($count === 1) {
                 $match = $matches[0];
                 $at = strlen($match[0]) + $match[1];
                 $spaces = $matches[1][0];
                 $content = substr($content, 0, $at) . "{$spaces}new {$bundleClass}(),\n" . substr($content, $at);
             }
             return $content;
         });
     }
     if ($arguments['update-routing']) {
         $builder->prepend('app/config/routing.yml', "{$arguments['alias']}:\n    resource: \"@{$arguments['name']}/Controller/\"\n    type:     annotation\n    prefix:   /\n\n");
     }
 }