<?php
namespace Pimcore\Bundle\GeneratorBundle\Command;
use Pimcore\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
use Pimcore\Bundle\GeneratorBundle\Generator\Generator;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
/**
* Base class for generator commands.
*
* The following class is copied from \Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand
*/
abstract class BaseGeneratorCommand extends ContainerAwareCommand
{
/**
* @var Generator
*/
private $generator;
abstract protected function createGenerator();
protected function getGenerator(BundleInterface $bundle = null)
{
if (null === $this->generator) {
$this->generator = $this->createGenerator();
$this->generator->setSkeletonDirs($this->getSkeletonDirs($bundle));
}
return $this->generator;
}
protected function getSkeletonDirs(BundleInterface $bundle = null)
{
$skeletonDirs = [];
if (isset($bundle) && is_dir($dir = $bundle->getPath().'/Resources/GeneratorBundle/skeleton')) {
$skeletonDirs[] = $dir;
}
if (is_dir($dir = $this->getContainer()->get('kernel')->getRootdir().'/Resources/GeneratorBundle/skeleton')) {
$skeletonDirs[] = $dir;
}
$skeletonDirs[] = __DIR__.'/../Resources/skeleton';
$skeletonDirs[] = __DIR__.'/../Resources';
return $skeletonDirs;
}
protected function getQuestionHelper()
{
$question = $this->getHelperSet()->get('question');
if (!$question || get_class($question) !== QuestionHelper::class) {
$this->getHelperSet()->set($question = new QuestionHelper());
}
return $question;
}
/**
* Tries to make a path relative to the project, which prints nicer.
*
* @param string $absolutePath
*
* @return string
*/
protected function makePathRelative($absolutePath)
{
$projectRootDir = dirname($this->getContainer()->getParameter('kernel.root_dir'));
return str_replace($projectRootDir.'/', '', realpath($absolutePath) ?: $absolutePath);
}
}