vendor/pimcore/pimcore/bundles/GeneratorBundle/Command/BaseGeneratorCommand.php line 15

Open in your IDE?
  1. <?php
  2. namespace Pimcore\Bundle\GeneratorBundle\Command;
  3. use Pimcore\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
  4. use Pimcore\Bundle\GeneratorBundle\Generator\Generator;
  5. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  6. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  7. /**
  8.  * Base class for generator commands.
  9.  *
  10.  * The following class is copied from \Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand
  11.  */
  12. abstract class BaseGeneratorCommand extends ContainerAwareCommand
  13. {
  14.     /**
  15.      * @var Generator
  16.      */
  17.     private $generator;
  18.     // only useful for unit tests
  19.     public function setGenerator(Generator $generator)
  20.     {
  21.         $this->generator $generator;
  22.     }
  23.     abstract protected function createGenerator();
  24.     protected function getGenerator(BundleInterface $bundle null)
  25.     {
  26.         if (null === $this->generator) {
  27.             $this->generator $this->createGenerator();
  28.             $this->generator->setSkeletonDirs($this->getSkeletonDirs($bundle));
  29.         }
  30.         return $this->generator;
  31.     }
  32.     protected function getSkeletonDirs(BundleInterface $bundle null)
  33.     {
  34.         $skeletonDirs = [];
  35.         if (isset($bundle) && is_dir($dir $bundle->getPath().'/Resources/GeneratorBundle/skeleton')) {
  36.             $skeletonDirs[] = $dir;
  37.         }
  38.         if (is_dir($dir $this->getContainer()->get('kernel')->getRootdir().'/Resources/GeneratorBundle/skeleton')) {
  39.             $skeletonDirs[] = $dir;
  40.         }
  41.         $skeletonDirs[] = __DIR__.'/../Resources/skeleton';
  42.         $skeletonDirs[] = __DIR__.'/../Resources';
  43.         return $skeletonDirs;
  44.     }
  45.     protected function getQuestionHelper()
  46.     {
  47.         $question $this->getHelperSet()->get('question');
  48.         if (!$question || get_class($question) !== 'Pimcore\Bundle\GeneratorBundle\Command\Helper\QuestionHelper') {
  49.             $this->getHelperSet()->set($question = new QuestionHelper());
  50.         }
  51.         return $question;
  52.     }
  53.     /**
  54.      * Tries to make a path relative to the project, which prints nicer.
  55.      *
  56.      * @param string $absolutePath
  57.      *
  58.      * @return string
  59.      */
  60.     protected function makePathRelative($absolutePath)
  61.     {
  62.         $projectRootDir dirname($this->getContainer()->getParameter('kernel.root_dir'));
  63.         return str_replace($projectRootDir.'/'''realpath($absolutePath) ?: $absolutePath);
  64.     }
  65. }