Beispiel #1
0
 protected function getGenerator(BundleInterface $bundle = null)
 {
     if (null === $this->generator) {
         $this->generator = $this->createGenerator();
         $this->generator->setSkeletonDirs($this->getSkeletonDirs($bundle));
     }
     return $this->generator;
 }
Beispiel #2
0
 /**
  * Adds a routing resource at the top of the existing ones.
  *
  * @param string $bundle
  * @param string $format
  * @param string $prefix
  * @param string $path
  *
  * @return bool Whether the operation succeeded
  *
  * @throws \RuntimeException If bundle is already imported
  */
 public function addResource($bundle, $format, $prefix = '/', $path = 'routing')
 {
     $current = '';
     $code = sprintf("%s:\n", $this->getImportedResourceYamlKey($bundle, $prefix));
     if (file_exists($this->file)) {
         $current = file_get_contents($this->file);
         // Don't add same bundle twice
         if (false !== strpos($current, '@' . $bundle)) {
             throw new \RuntimeException(sprintf('Bundle "%s" is already imported.', $bundle));
         }
     } elseif (!is_dir($dir = dirname($this->file))) {
         Generator::mkdir($dir);
     }
     if ('annotation' == $format) {
         $code .= sprintf("    resource: \"@%s/Controller/\"\n    type:     annotation\n", $bundle);
     } else {
         $code .= sprintf("    resource: \"@%s/Resources/config/%s.%s\"\n", $bundle, $path, $format);
     }
     $code .= sprintf("    prefix:   %s\n", $prefix);
     $code .= "\n";
     $code .= $current;
     if (false === Generator::dump($this->file, $code)) {
         return false;
     }
     return true;
 }
 /**
  * Adds a configuration resource at the top of the existing ones.
  *
  * @param Bundle $bundle
  *
  * @throws \RuntimeException If this process fails for any reason
  */
 public function addResource(Bundle $bundle)
 {
     // if the config.yml file doesn't exist, don't even try.
     if (!file_exists($this->file)) {
         throw new \RuntimeException(sprintf('The target config file %s does not exist', $this->file));
     }
     $code = $this->getImportCode($bundle);
     $currentContents = file_get_contents($this->file);
     // Don't add same bundle twice
     if (false !== strpos($currentContents, $code)) {
         throw new \RuntimeException(sprintf('The %s configuration file from %s is already imported', $bundle->getServicesConfigurationFilename(), $bundle->getName()));
     }
     // find the "imports" line and add this at the end of that list
     $lastImportedPath = $this->findLastImportedPath($currentContents);
     if (!$lastImportedPath) {
         throw new \RuntimeException(sprintf('Could not find the imports key in %s', $this->file));
     }
     // find imports:
     $importsPosition = strpos($currentContents, 'imports:');
     // find the last import
     $lastImportPosition = strpos($currentContents, $lastImportedPath, $importsPosition);
     // find the line break after the last import
     $targetLinebreakPosition = strpos($currentContents, "\n", $lastImportPosition);
     $newContents = substr($currentContents, 0, $targetLinebreakPosition) . "\n" . $code . substr($currentContents, $targetLinebreakPosition);
     if (false === Generator::dump($this->file, $newContents)) {
         throw new \RuntimeException(sprintf('Could not write file %s ', $this->file));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function setSkeletonDirs($skeletonDirs)
 {
     $this->skeletonDirs = is_array($skeletonDirs) ? $skeletonDirs : array($skeletonDirs);
     $this->bcEnabled = false;
     if (self::getGeneratorVersion() === '2.2') {
         $this->bcEnabled = true;
     }
     if (self::getGeneratorVersion() === '2.3') {
         parent::setSkeletonDirs($this->skeletonDirs);
     }
 }
Beispiel #5
0
 /**
  * Adds a bundle at the end of the existing ones.
  *
  * @param string $bundle The bundle class name
  *
  * @return bool Whether the operation succeeded
  *
  * @throws \RuntimeException If bundle is already defined
  */
 public function addBundle($bundle)
 {
     if (!$this->getFilename()) {
         return false;
     }
     $src = file($this->getFilename());
     $method = $this->reflected->getMethod('registerBundles');
     $lines = array_slice($src, $method->getStartLine() - 1, $method->getEndLine() - $method->getStartLine() + 1);
     // Don't add same bundle twice
     if (false !== strpos(implode('', $lines), $bundle)) {
         throw new \RuntimeException(sprintf('Bundle "%s" is already defined in "AppKernel::registerBundles()".', $bundle));
     }
     $this->setCode(token_get_all('<?php ' . implode('', $lines)), $method->getStartLine());
     while ($token = $this->next()) {
         // $bundles
         if (T_VARIABLE !== $token[0] || '$bundles' !== $token[1]) {
             continue;
         }
         // =
         $this->next();
         // array start with traditional or short syntax
         $token = $this->next();
         if (T_ARRAY !== $token[0] && '[' !== $this->value($token)) {
             return false;
         }
         // add the bundle at the end of the array
         while ($token = $this->next()) {
             // look for ); or ];
             if (')' !== $this->value($token) && ']' !== $this->value($token)) {
                 continue;
             }
             if (';' !== $this->value($this->peek())) {
                 continue;
             }
             $this->next();
             $leadingContent = implode('', array_slice($src, 0, $this->line));
             // trim semicolon
             $leadingContent = rtrim(rtrim($leadingContent), ';');
             // We want to match ) & ]
             $closingSymbolRegex = '#(\\)|])$#';
             // get closing symbol used
             preg_match($closingSymbolRegex, $leadingContent, $matches);
             $closingSymbol = $matches[0];
             // remove last close parentheses
             $leadingContent = rtrim(preg_replace($closingSymbolRegex, '', rtrim($leadingContent)));
             if ('(' !== substr($leadingContent, -1) && '[' !== substr($leadingContent, -1)) {
                 // end of leading content is not open parentheses or bracket, then assume that array contains at least one element
                 $leadingContent = rtrim($leadingContent, ',') . ',';
             }
             $lines = array_merge(array($leadingContent, "\n"), array(str_repeat(' ', 12), sprintf('new %s(),', $bundle), "\n"), array(str_repeat(' ', 8), $closingSymbol . ';', "\n"), array_slice($src, $this->line));
             Generator::dump($this->getFilename(), implode('', $lines));
             return true;
         }
     }
 }
Beispiel #6
0
 /**
  * configure available skeletons twig files.
  */
 public function setSkeletonDirs($skeletonDirs)
 {
     parent::setSkeletonDirs($skeletonDirs);
     $this->skeletonDirs = $skeletonDirs;
 }
Beispiel #7
0
 public function __construct(Filesystem $filesystem, $skeletonDir)
 {
     parent::__construct();
     $this->filesystem = $filesystem;
     $this->skeletonDir = $skeletonDir;
 }