public function generate($namespace, $bundle, $dir, $format) { // $dir .= '/'.strtr($namespace, '\\', '/'); if (file_exists($dir)) { if (!is_dir($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" exists but is a file.', realpath($dir))); } // $files = scandir($dir); // if ($files != array('.', '..', 'composer.json', 'composer.lock', 'composer.phar', 'parameters.yml', 'vendor')) { // throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not empty.', realpath($dir))); // } if (!is_writable($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not writable.', realpath($dir))); } } $basename = substr($bundle, 0, -6); $parameters = array('namespace' => $namespace, 'bundle' => $bundle, 'format' => $format, 'bundle_basename' => $basename, 'extension_alias' => Container::underscore($basename)); $this->renderFile('bundle/Bundle.php.twig', $dir . '/' . $bundle . '.php', $parameters); $this->renderFile('bundle/Extension.php.twig', $dir . '/DependencyInjection/' . $basename . 'Extension.php', $parameters); $this->renderFile('bundle/Configuration.php.twig', $dir . '/DependencyInjection/Configuration.php', $parameters); $this->renderFile('bundle/DefaultController.php.twig', $dir . '/Controller/DefaultController.php', $parameters); $this->renderFile('bundle/DefaultControllerTest.php.twig', $dir . '/Tests/Controller/DefaultControllerTest.php', $parameters); $this->renderFile('bundle/index.html.twig.twig', $dir . '/Resources/views/Default/index.html.twig', $parameters); if ('xml' === $format || 'annotation' === $format) { $this->renderFile('bundle/services.xml.twig', $dir . '/Resources/config/services.xml', $parameters); } else { $this->renderFile('bundle/services.' . $format . '.twig', $dir . '/Resources/config/services.' . $format, $parameters); } if ('annotation' != $format) { $this->renderFile('bundle/routing.' . $format . '.twig', $dir . '/Resources/config/routing.' . $format, $parameters); } }
public function generate($namespace, $bundle, $dir, $format, $structure) { $dir .= '/' . str_replace('\\', '/', $namespace); if (file_exists($dir)) { throw new \RuntimeException(sprintf('Le repertoire du bundle existe deja.', realpath($dir))); } $basename = substr($bundle, 0, -6); $parameters = array('namespace' => $namespace, 'bundle' => $bundle, 'format' => $format, 'bundle_basename' => $basename, 'extension_alias' => Container::underscore($basename)); $this->renderFile($this->skeletonDir, 'Bundle.php', $dir . '/' . $bundle . '.php', $parameters); $this->renderFile($this->skeletonDir, 'Extension.php', $dir . '/DependencyInjection/' . $basename . 'Extension.php', $parameters); $this->renderFile($this->skeletonDir, 'Configuration.php', $dir . '/DependencyInjection/Configuration.php', $parameters); $this->renderFile($this->skeletonDir, 'DefaultController.php', $dir . '/Controller/DefaultController.php', $parameters); $this->renderFile($this->skeletonDir, 'index.html.twig', $dir . '/Resources/views/Default/index.html.twig', $parameters); $this->renderFile($this->skeletonDir, 'services.xml', $dir . '/Resources/config/services.xml', $parameters); if ($structure) { $this->filesystem->mkdir($dir . '/Resources/doc'); $this->filesystem->touch($dir . '/Resources/doc/index.rst'); $this->filesystem->mkdir($dir . '/Resources/translations'); $this->filesystem->copy($this->skeletonDir . '/messages.fr.yml', $dir . '/Resources/translations/messages.fr.yml'); $this->filesystem->mkdir($dir . '/Resources/public/css'); $this->filesystem->mkdir($dir . '/Resources/public/images'); $this->filesystem->mkdir($dir . '/Resources/public/js'); $this->filesystem->mkdir($dir . '/Tests'); $this->filesystem->mkdir($dir . '/Tests/Controller'); $this->filesystem->mkdir($dir . '/Tests/Entity'); $target = $dir . '/Resources/doc/index.md'; $this->renderFile($this->skeletonDir, 'index.md', $target, array('creationdate' => date("d/m/Y"), 'author' => 'Lowbi', 'bundleName' => $bundle)); } }
/** * Adds a routing resource at the top of the existing ones. * * @param string $bundle * @param string $format * @param string $prefix * @param string $path * * @return Boolean true if it worked, false otherwise * * @throws \RuntimeException If bundle is already imported */ public function addResource($bundle, $format, $prefix = null, $path = 'routing') { $current = ''; if (null === $prefix) { $prefix = '/admin/' . Container::underscore($bundle) . ($this->yaml_prefix ? '/' . $this->yaml_prefix : ''); } $routing_name = $bundle . ('/' !== $prefix ? '_' . str_replace('/', '_', substr($prefix, 1)) : ''); if (file_exists($this->file)) { $current = file_get_contents($this->file); // Don't add same bundle twice if (false !== strpos($current, $routing_name)) { throw new \RuntimeException(sprintf('Bundle "%s" is already imported.', $bundle)); } } elseif (!is_dir($dir = dirname(realpath($this->file)))) { mkdir($dir, 0777, true); } $code = sprintf("%s:\n", $routing_name); if ('admingenerator' == $format) { $code .= sprintf(" resource: \"@%s/Controller/%s\"\n type: admingenerator\n", $bundle, $this->yaml_prefix ? ucfirst($this->yaml_prefix) . '/' : ''); } 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 === file_put_contents($this->file, $code)) { return false; } return true; }
/** * Returns the bundle's container extension. * * @return ExtensionInterface|null The container extension * * @api */ public function getContainerExtension() { if (null === $this->extension) { $basename = preg_replace('/Bundle$/', '', $this->getName()); $class = $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension'; if (class_exists($class)) { $extension = new $class(); // check naming convention $expectedAlias = Container::underscore($basename); if ($expectedAlias != $extension->getAlias()) { throw new \LogicException(sprintf( 'The extension alias for the default extension of a '. 'bundle must be the underscored version of the '. 'bundle name ("%s" instead of "%s")', $expectedAlias, $extension->getAlias() )); } $this->extension = $extension; } else { $this->extension = false; } } if ($this->extension) { return $this->extension; } }
/** * Returns the bundle's container extension. * * @return ExtensionInterface|null The container extension * * @throws \LogicException */ public function getContainerExtension() { if (null === $this->extension) { $extension = $this->createContainerExtension(); if (null !== $extension) { if (!$extension instanceof ExtensionInterface) { throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension))); } // check naming convention $basename = preg_replace('/Bundle$/', '', $this->getName()); $expectedAlias = Container::underscore($basename); if ($expectedAlias != $extension->getAlias()) { throw new \LogicException(sprintf( 'Users will expect the alias of the default extension of a bundle to be the underscored version of the bundle name ("%s"). You can override "Bundle::getContainerExtension()" if you want to use "%s" or another alias.', $expectedAlias, $extension->getAlias() )); } $this->extension = $extension; } else { $this->extension = false; } } if ($this->extension) { return $this->extension; } }
/** * {@inheritdoc} */ public function generateExt($namespace, $bundle, $dir, $format, $structure, array $options) { $format = 'annotation'; // @codeCoverageIgnoreStart if (null === $this->bundleSkeletonDir) { $this->bundleSkeletonDir = __DIR__ . '/../../../../../../vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Resources/skeleton'; if (!file_exists($this->bundleSkeletonDir)) { $this->bundleSkeletonDir = __DIR__ . '/../../../../../../../sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Resources/skeleton'; } } // @codeCoverageIgnoreEnd $this->setSkeletonDirs($this->bundleSkeletonDir); $this->generate($namespace, $bundle, $dir, $format, $structure); $dir .= '/' . strtr($namespace, '\\', '/'); $themeBasename = str_replace('Bundle', '', $bundle); $extensionAlias = Container::underscore($themeBasename); $themeSkeletonDir = __DIR__ . '/../../Resources/skeleton/app-theme'; $this->setSkeletonDirs($themeSkeletonDir); $parameters = array('namespace_path' => str_replace('\\', '\\\\', $namespace), 'target_dir' => str_replace('\\', '/', $namespace), 'bundle' => $bundle, 'theme_basename' => $themeBasename, 'extension_alias' => $extensionAlias); $this->renderFile('theme.xml', $dir . '/Resources/config/' . $extensionAlias . '.xml', $parameters); $this->renderFile('info.yml', $dir . '/Resources/data/info.yml', $parameters); $this->renderFile('autoload.json', $dir . '/autoload.json', $parameters); $this->renderFile('composer.json', $dir . '/composer.json', $parameters); $this->filesystem->mkdir($dir . '/Resources/views/Theme'); $this->filesystem->mkdir($dir . '/Resources/views/Slots'); }
/** * {@inheritdoc} */ public function generateExt($namespace, $bundle, $dir, $format, $structure, array $options) { $format = 'annotation'; // @codeCoverageIgnoreStart if (null === $this->bundleSkeletonDir) { $this->bundleSkeletonDir = __DIR__ . '/../../../../../../vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Resources/skeleton'; if (!file_exists($this->bundleSkeletonDir)) { $this->bundleSkeletonDir = __DIR__ . '/../../../../../../../sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Resources/skeleton'; } } // @codeCoverageIgnoreEnd $this->setSkeletonDirs($this->bundleSkeletonDir); $this->generate($namespace, $bundle, $dir, $format, $structure); $dir .= '/' . strtr($namespace, '\\', '/'); $bundleBasename = str_replace('Bundle', '', $bundle); $this->filesystem->mkdir($dir . '/Core/Block'); $extensionAlias = Container::underscore($bundleBasename); $typeLowercase = strtolower($bundleBasename); $parameters = array('namespace' => $namespace, 'namespace_path' => str_replace('\\', '\\\\', $namespace), 'target_dir' => str_replace('\\', '/', $namespace), 'bundle' => $bundle, 'format' => $format, 'bundle_basename' => $bundleBasename, 'type_lowercase' => $typeLowercase, 'extension_alias' => $extensionAlias, 'description' => $options["description"], 'group' => $options["group"]); $blockSkeletonDir = __DIR__ . '/../../Resources/skeleton/app-block'; $this->setSkeletonDirs($blockSkeletonDir); $this->renderFile('Block.php', $dir . '/Core/Block/BlockManager' . $bundleBasename . '.php', $parameters); $this->renderFile('FormType.php', $dir . '/Core/Form/' . $bundleBasename . 'Type.php', $parameters); $this->renderFile('app_block.xml', $dir . '/Resources/config/app_block.xml', $parameters); $this->renderFile('config_rkcms.yml', $dir . '/Resources/config/config_rkcms.yml', $parameters); $this->renderFile('config_rkcms_dev.yml', $dir . '/Resources/config/config_rkcms_dev.yml', $parameters); $this->renderFile('config_rkcms_test.yml', $dir . '/Resources/config/config_rkcms_test.yml', $parameters); $this->renderFile('autoload.json', $dir . '/autoload.json', $parameters); if (!array_key_exists("no-strict", $options) || $options["no-strict"] == false) { $this->renderFile('composer.json', $dir . '/composer.json', $parameters); } $this->filesystem->copy($blockSkeletonDir . '/block.html.twig', $dir . '/Resources/views/Content/' . $typeLowercase . '.html.twig'); $this->filesystem->copy($blockSkeletonDir . '/form_editor.html.twig', $dir . '/Resources/views/Editor/' . $typeLowercase . '.html.twig'); }
/** * 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 true if it worked, false otherwise * * @throws \RuntimeException If bundle is already imported */ public function addResource($bundle, $format, $prefix = '/', $path = 'routing') { $current = ''; 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))) { mkdir($dir, 0777, true); } $bundleName = substr($bundle, 0, -6); $prefixName = '/' !== $prefix ? '_' . str_replace('/', '_', substr($prefix, 1)) : ''; $prefixName = strpos($bundleName, $prefixName) !== false ? $prefixName : ''; $code = sprintf("%s:\n", Container::underscore($bundleName) . $prefixName); 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 === file_put_contents($this->file, $code)) { return false; } return true; }
public function generate($namespace, $bundle, $dir, $format, $structure) { $dir .= '/' . strtr($namespace, '\\', '/'); if (file_exists($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not empty.', realpath($dir))); } $basename = substr($bundle, 0, -6); $parameters = array('namespace' => $namespace, 'bundle' => $bundle, 'format' => $format, 'bundle_basename' => $basename, 'extension_alias' => Container::underscore($basename)); $this->renderFile($this->skeletonDir, 'Bundle.php', $dir . '/' . $bundle . '.php', $parameters); $this->renderFile($this->skeletonDir, 'Extension.php', $dir . '/DependencyInjection/' . $basename . 'Extension.php', $parameters); $this->renderFile($this->skeletonDir, 'Configuration.php', $dir . '/DependencyInjection/Configuration.php', $parameters); $this->renderFile($this->skeletonDir, 'DefaultController.php', $dir . '/Controller/DefaultController.php', $parameters); $this->renderFile($this->skeletonDir, 'DefaultControllerTest.php', $dir . '/Tests/Controller/DefaultControllerTest.php', $parameters); $this->renderFile($this->skeletonDir, 'index.html.twig', $dir . '/Resources/views/Default/index.html.twig', $parameters); if ('xml' === $format || 'annotation' === $format) { $this->renderFile($this->skeletonDir, 'services.xml', $dir . '/Resources/config/services.xml', $parameters); } else { $this->renderFile($this->skeletonDir, 'services.' . $format, $dir . '/Resources/config/services.' . $format, $parameters); } if ('annotation' != $format) { $this->renderFile($this->skeletonDir, 'routing.' . $format, $dir . '/Resources/config/routing.' . $format, $parameters); } if ($structure) { $this->filesystem->mkdir($dir . '/Resources/doc'); $this->filesystem->touch($dir . '/Resources/doc/index.rst'); $this->filesystem->mkdir($dir . '/Resources/translations'); $this->filesystem->copy($this->skeletonDir . '/messages.fr.xliff', $dir . '/Resources/translations/messages.fr.xliff'); $this->filesystem->mkdir($dir . '/Resources/public/css'); $this->filesystem->mkdir($dir . '/Resources/public/images'); $this->filesystem->mkdir($dir . '/Resources/public/js'); } }
/** * Convert a class name to a standardized symfony service name * 'Acme\FooBundle\Bar\FooBar' => 'acme.foo_bundle.bar.foo_bar' * @param $classname * @return string */ public function generateServiceNameFromClassName($classname) { $classname = str_replace(['\\', '_'], '.', $classname); // @todo in Core-2.0 the '_' can be removed. $classname = Container::underscore($classname); return trim($classname, "\\_. \t\n\r\v"); }
public function generate($namespace, $bundle, $dir, $format, $structure) { $dir .= '/' . strtr($namespace, '\\', '/'); if (file_exists($dir)) { if (!is_dir($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" exists but is a file.', realpath($dir))); } $files = scandir($dir); if ($files != array('.', '..')) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not empty.', realpath($dir))); } if (!is_writable($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not writable.', realpath($dir))); } } $basename = substr($bundle, 0, -6); $parameters = array('namespace' => $namespace, 'bundle' => $bundle, 'format' => $format, 'bundle_basename' => $basename, 'extension_alias' => Container::underscore($basename)); $this->renderFile('bundle/Bundle.php.twig', $dir . '/' . $bundle . '.php', $parameters); $this->renderFile('bundle/Controller.php.twig', $dir . '/Controller/' . $basename . 'Controller.php', $parameters); $this->renderFile('bundle/Entity.php.twig', $dir . '/Entity/' . $basename . '.php', $parameters); $this->renderFile('bundle/ControllerTest.php.twig', $dir . '/Tests/Controller/' . $basename . 'ControllerTest.php', $parameters); if ('xml' === $format || 'annotation' === $format) { $this->renderFile('bundle/services.xml.twig', $dir . '/Resources/config/services.xml', $parameters); } else { $this->renderFile('bundle/services.' . $format . '.twig', $dir . '/Resources/config/services.' . $format, $parameters); } if ('annotation' != $format) { $this->renderFile('bundle/routing.' . $format . '.twig', $dir . '/Resources/config/routing.' . $format, $parameters); } }
/** * generate bundle code * * @param string $namespace namspace name * @param string $bundle bundle name * @param string $dir bundle dir * @param string $format bundle condfig file format * * @return void */ public function generate($namespace, $bundle, $dir, $format) { $dir .= '/' . strtr($namespace, '\\', '/'); if (file_exists($dir)) { if (!is_dir($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" exists but is a file.', realpath($dir))); } $files = scandir($dir); if ($files != array('.', '..')) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not empty.', realpath($dir))); } if (!is_writable($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not writable.', realpath($dir))); } } $basename = $this->getBundleBaseName($bundle); $parameters = array('namespace' => $namespace, 'bundle' => $bundle, 'format' => $format, 'bundle_basename' => $basename, 'extension_alias' => Container::underscore($basename)); $this->renderFile('bundle/Bundle.php.twig', $dir . '/' . $bundle . '.php', $parameters); $this->renderFile('bundle/Extension.php.twig', $dir . '/DependencyInjection/' . $basename . 'Extension.php', $parameters); if ('xml' === $format || 'annotation' === $format) { // @todo make this leave doctrine alone and move doctrine to a Manipulator in generate:resource $this->renderFile('bundle/services.xml.twig', $dir . '/Resources/config/services.xml', $parameters); mkdir($dir . '/Resources/config/doctrine'); $this->renderFile('bundle/config.xml.twig', $dir . '/Resources/config/config.xml', $parameters); } else { $this->renderFile('bundle/services.' . $format . '.twig', $dir . '/Resources/config/services.' . $format, $parameters); mkdir($dir . '/Resources/config/doctrine'); $this->renderFile('bundle/config.' . $format . '.twig', $dir . '/Resources/config/config.' . $format, $parameters); } if ('annotation' != $format) { $this->renderFile('bundle/routing.' . $format . '.twig', $dir . '/Resources/config/routing.' . $format, $parameters); } }
/** * Returns the recommended alias to use in XML. * * This alias is also the mandatory prefix to use when using YAML. * * @return string The alias */ public function getAlias() { $className = get_class($this); if (substr($className, -9) != 'Extension') { throw new \BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.'); } $classBaseName = substr(strrchr($className, '\\'), 1, -9); return Container::underscore($classBaseName); }
private function getShortNameForBundle($bundleName) { $shortName = $bundleName; if (mb_substr($bundleName, -6) === 'Bundle') { $shortName = mb_substr($shortName, 0, -6); } // this is used by SensioGenerator bundle when generating extension name from bundle name return Container::underscore($shortName); }
/** * @see \Symfony\Component\HttpKernel\Bundle::registerCommands * * @param ContainerBuilder $container */ public function process(ContainerBuilder $container) { if (!$container->hasParameter('bangpound_console.default_application_id')) { return; } $count = 0; $defaultApplicationId = $container->getParameter('bangpound_console.default_application_id'); // Identify classes of already tagged Command services // so this pass does not create additional service definitions // for them. $classes = array_map(function ($id) use($container) { $class = $container->getDefinition($id)->getClass(); return $container->getParameterBag()->resolveValue($class); }, array_keys($container->findTaggedServiceIds('console.command'))); /** @var BundleInterface $bundle */ foreach ($this->bundles as $bundle) { if (!is_dir($dir = $bundle->getPath() . '/Command')) { continue; } $finder = new Finder(); $finder->files()->name('*Command.php')->in($dir); $prefix = $bundle->getNamespace() . '\\Command'; /** @var SplFileInfo $file */ foreach ($finder as $file) { $ns = $prefix; if ($relativePath = $file->getRelativePath()) { $ns .= '\\' . strtr($relativePath, '/', '\\'); } $class = $ns . '\\' . $file->getBasename('.php'); // This command is already in the container. if (in_array($class, $classes)) { continue; } $r = new \ReflectionClass($class); if ($r->isSubclassOf(self::COMMAND_CLASS) && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) { $name = Container::underscore(preg_replace('/Command/', '', $r->getShortName())); $name = strtolower(str_replace('\\', '_', $name)); if (!$bundle->getContainerExtension()) { $alias = preg_replace('/Bundle$/', '', $bundle->getName()); $alias = Container::underscore($alias); } else { $alias = $bundle->getContainerExtension()->getAlias(); } $id = $alias . '.command.' . $name; if ($container->hasDefinition($id)) { $id = sprintf('%s_%d', hash('sha256', $file), ++$count); } $definition = $container->register($id, $r->getName()); $definition->addTag('console.command', ['application' => $defaultApplicationId]); if ($r->isSubclassOf('Symfony\\Component\\DependencyInjection\\ContainerAwareInterface')) { $definition->addMethodCall('setContainer', [new Reference('service_container')]); } } } } }
/** * Generates the template file * * @param string $dir The directory where the generated file must be saved * @param string $themeName * @param string $templateName * @param array $templateSlots An array of slot names * @return string A message formatted to be displayed on the console */ public function generateTemplate($dir, $themeName, $templateName, array $templateSlots) { $themeBasename = str_replace('Bundle', '', $themeName); $extensionAlias = Container::underscore($themeBasename); $parameters = array('theme_name' => $themeName, 'template_name' => $templateName, 'extension_alias' => $extensionAlias, 'template_slots' => $templateSlots); $templateFile = $templateName . '.xml'; $this->setSkeletonDirs($this->themeSkeletonDir); $this->renderFile('template.xml', $dir . '/' . $templateFile, $parameters); return sprintf('The template <info>%s</info> has been generated into <info>%s</info>', $templateFile, $dir); }
public function getModels($name) { if (substr($name, 0, 3) === 'get') { // getCamelCase -> camel_case $name = Container::underscore(substr($name, 3)); } if (!isset($this->modelLoaders[$name])) { throw new \InvalidArgumentException("Could not find models of type {$name}."); } return $this->modelLoaders[$name]->getModels(); }
private function buildOrmCompilerPass(ContainerBuilder $container, array $mappings) { if (!class_exists('Doctrine\\ORM\\Version')) { return; } $doctrineOrmCompiler = $this->findDoctrineOrmCompiler(); if (!$doctrineOrmCompiler) { return; } $bundleNameUnderscored = Container::underscore($this->getBundleBasename()); $container->addCompilerPass($doctrineOrmCompiler::createXmlMappingDriver($mappings, array($bundleNameUnderscored . '.model_manager_name'), $bundleNameUnderscored . '.backend_type_orm', array($this->getName() => $this->getNamespace() . '\\Model'))); }
/** * Builds the bundle. * * It is only ever called once when the cache is empty. * * The default implementation automatically registers a DIC extension * if its name is the same as the bundle name after replacing the * Bundle suffix by Extension (DependencyInjection\SensioBlogExtension * for a SensioBlogBundle for instance). In such a case, the alias * is forced to be the underscore version of the bundle name * (sensio_blog for a SensioBlogBundle for instance). * * This method can be overridden to register compilation passes, * other extensions, ... * * @param ContainerBuilder $container A ContainerBuilder instance */ public function build(ContainerBuilder $container) { $class = $this->getNamespace() . '\\DependencyInjection\\' . str_replace('Bundle', 'Extension', $this->getName()); if (class_exists($class)) { $extension = new $class(); $alias = Container::underscore(str_replace('Bundle', '', $this->getName())); if ($alias !== $extension->getAlias()) { throw new \LogicException(sprintf('The extension alias for the default extension of a bundle must be the underscored version of the bundle name ("%s" vs "%s")', $alias, $extension->getAlias())); } $container->registerExtension($extension); } }
/** * {@inheritDoc} */ public function getAlias() { try { return parent::getAlias(); } catch (BadMethodCallException $e) { $className = get_class($this); if (substr($className, -16) != '\\BundleExtension') { throw $e; } $aliasBaseName = str_replace(array('FancyGuy', '\\Bundle\\', '\\BundleExtension'), array('Fancyguy', '', ''), $className); return Container::underscore($aliasBaseName); } }
/** * build WidgetBundle files. * * @param string $namespace * @param string $format */ public function generate($namespace, $bundle, $dir, $format, $structure, $fields = null, $parent = null, $packagistParentName = null, $contentResolver = false, $parentContentResolver = false, $orgname = null, $cache = false) { $dir .= '/' . strtr($namespace, '\\', '/'); if (file_exists($dir)) { if (!is_dir($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" exists but is a file.', realpath($dir))); } $files = scandir($dir); if ($files !== ['.', '..']) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not empty.', realpath($dir))); } if (!is_writable($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not writable.', realpath($dir))); } } $basename = substr($bundle, 0, -6); $widget = str_replace('VictoireWidget', '', $basename); $_fields = []; foreach ($fields as $_field) { $_fields[] = $_field['fieldName']; } //guess the toString property we could use $toStringProperty = 'id'; if (in_array('name', $_fields)) { $toStringProperty = 'name'; } elseif (in_array('title', $_fields)) { $toStringProperty = 'title'; } elseif (in_array('description', $_fields)) { $toStringProperty = 'description'; } $parameters = ['namespace' => $namespace, 'bundle' => $bundle, 'parent' => $parent, 'packagistParentName' => $packagistParentName, 'orgname' => $orgname, 'widget' => $widget, 'format' => $format, 'fields' => $fields, 'toStringProperty' => $toStringProperty, 'bundle_basename' => $basename, 'content_resolver' => $contentResolver, 'cache' => $cache, 'parent_content_resolver' => $parentContentResolver, 'extension_alias' => Container::underscore($basename)]; $this->renderFile('bundle/Bundle.php.twig', $dir . '/' . $bundle . '.php', $parameters); $this->renderFile('README.md.twig', $dir . '/README.md', $parameters); $this->renderFile('composer.json.twig', $dir . '/composer.json', $parameters); $this->renderFile('bundle/Extension.php.twig', $dir . '/DependencyInjection/' . $basename . 'Extension.php', array_merge($parameters, ['format' => 'yml'])); $this->renderFile('bundle/Configuration.php.twig', $dir . '/DependencyInjection/Configuration.php', $parameters); $this->renderFile('widget/entity.php.twig', $dir . '/Entity/Widget' . $widget . '.php', $parameters); $this->renderFile('widget/form.php.twig', $dir . '/Form/Widget' . $widget . 'Type.php', $parameters); $this->renderFile('widget/config.yml.twig', $dir . '/Resources/config/config.yml', $parameters); $this->renderFile('widget/services.yml.twig', $dir . '/Resources/config/services.yml', $parameters); $this->renderFile('widget/victoire.xliff.twig', $dir . '/Resources/translations/victoire.en.xliff', $parameters); $this->renderFile('widget/victoire.xliff.twig', $dir . '/Resources/translations/victoire.fr.xliff', $parameters); //Generate new and Edit views $this->renderFile('widget/views/new.html.twig.twig', $dir . '/Resources/views/new.html.twig', $parameters); $this->renderFile('widget/views/edit.html.twig.twig', $dir . '/Resources/views/edit.html.twig', $parameters); $this->renderFile('widget/views/show.html.twig.twig', $dir . '/Resources/views/show.html.twig', $parameters); if ($contentResolver) { $parameters['parentResolver'] = class_exists('Victoire\\Widget\\' . $parent . 'Bundle\\Widget\\Resolver\\Widget' . $parent . 'ContentResolver'); $this->renderFile('widget/ContentResolver.php.twig', $dir . '/Resolver/Widget' . $widget . 'ContentResolver.php', $parameters); } }
/** * @param BundleInterface $bundle * @param string $entity * @param BundleInterface $entityBundle * @param OutputInterface $output */ public function generateCrud(BundleInterface $bundle, $entity, BundleInterface $entityBundle, OutputInterface $output) { $parameters = array('namespace' => $bundle->getNamespace(), 'bundle' => $bundle->getName(), 'entity' => $entity, 'entity_pluralized' => lcfirst(Inflector::pluralize($entity)), 'entity_namespace' => $entityBundle->getNamespace() . '\\Entity', 'entity_bundle' => $entityBundle->getName(), 'route_prefix' => Container::underscore(substr($bundle->getName(), 0, -6)) . '_' . Container::underscore($entity)); // Render php files $output->writeln($this->renderTwigFile('crud/Controller.php.twig', $bundle->getPath() . '/Controller/' . $entity . 'Controller.php', $parameters)); $output->writeln($this->renderTwigFile('crud/FormType.php.twig', $bundle->getPath() . '/Form/' . $entity . 'Type.php', $parameters)); $output->writeln($this->renderTwigFile('crud/FilterFormType.php.twig', $bundle->getPath() . '/Form/' . $entity . 'FilterType.php', $parameters)); // Render twig files $output->writeln($this->renderTwigFile('crud/views/index.html.twig.twig', $bundle->getPath() . '/Resources/views/' . lcfirst($entity) . '/index.html.twig', $parameters)); $output->writeln($this->renderTwigFile('crud/views/create.html.twig.twig', $bundle->getPath() . '/Resources/views/' . lcfirst($entity) . '/create.html.twig', $parameters)); $output->writeln($this->renderTwigFile('crud/views/update.html.twig.twig', $bundle->getPath() . '/Resources/views/' . lcfirst($entity) . '/update.html.twig', $parameters)); $output->writeln($this->renderTwigFile('crud/views/delete.html.twig.twig', $bundle->getPath() . '/Resources/views/' . lcfirst($entity) . '/delete.html.twig', $parameters)); $output->writeln($this->renderTwigFile('crud/views/parts/item.html.twig.twig', $bundle->getPath() . '/Resources/views/' . lcfirst($entity) . '/parts/item.html.twig', $parameters)); }
/** * Adds a Manager service within the managers config file. * * @param BundleInterface $bundle * @param string $entity * * @return Boolean true if it worked, false otherwise * * @throws \RuntimeException If bundle is already imported */ public function addResource(BundleInterface $bundle, $entity) { $managerService = sprintf('%s.manager.%s', Container::underscore(substr($bundle->getName(), 0, -6)), strtolower($entity)); $managerClass = sprintf('%s\\Manager\\%sManager', $bundle->getNamespace(), ucfirst($entity)); $entityClass = sprintf('%s\\Entity\\%s', $bundle->getNamespace(), ucfirst($entity)); $content = Yaml::parse(file_get_contents($this->file)); $content['parameters'][$managerService . '.class'] = $managerClass; $content['services'][$managerService] = array('class' => "%{$managerService}.class%", 'tags' => array(array('name' => 'voryx.manager')), 'arguments' => array($entityClass)); $yaml = Yaml::dump($content, 4); if (false === file_put_contents($this->file, $yaml)) { return false; } return true; }
/** * Generates the extension file * * @param string $namespace * @param string $dir * @param string $themeName * @param array $templates */ public function generateExtension($namespace, $dir, $themeName, array $templates) { $themeBasename = str_replace('Bundle', '', $themeName); $extensionAlias = Container::underscore($themeBasename); $templateFiles = array_map(function ($template) { return basename($template["name"], '.html.twig'); }, $templates); $parameters = array('namespace' => $namespace, 'bundle_basename' => $themeBasename, 'theme_files' => array($extensionAlias), 'template_files' => $templateFiles, "extension_alias" => $extensionAlias); $this->setSkeletonDirs($this->themeSkeletonDir); $extensionFile = str_replace('Bundle', '', $themeBasename) . 'Extension.php'; $this->renderFile('Extension.php', $dir . '/' . $extensionFile, $parameters); $message = sprintf('The extension file <info>%s</info> has been generated into <info>%s</info>', $extensionFile, $dir); return $message; }
/** * {@inheritDoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $kernel = $this->getApplication()->getKernel(); $bundle = $kernel->getBundle('App'); $name = $input->getArgument('name'); $class = $input->getOption('class') ?: sprintf('%sExtension', $this->classify($name)); $namespace = sprintf('%s\\Twig', $bundle->getNamespace()); $fqcn = sprintf('%s\\%s', $namespace, $class); $twig = $this->createTwig(); $dialog = $this->getHelperSet()->get('dialog'); // generate the class $classPath = sprintf('%s/Twig/%s.php', $bundle->getPath(), $class); $classData = $twig->render('extension.php.twig', array('namespace' => $namespace, 'class' => $class, 'name' => $name)); $output->writeLn(sprintf('- <comment>%s</comment> extension:', $fqcn)); if (file_exists($classPath)) { $output->writeLn(' class <info>already exists</info>'); } else { $this->writeFile($classPath, $classData); $output->writeLn(' class <info>generated</info>'); } // generate the service definition $servicesPath = sprintf('%s/config/services.yml', $bundle->getPath()); $output->writeLn(sprintf("\n" . '- <comment>app.twig.%s_extension</comment> service:', strtolower($name))); if (file_exists($servicesPath)) { $servicesData = $twig->render('services.yml.twig', array('name' => $name, 'fqcn' => $fqcn, 'bundle_alias' => Container::underscore($bundle->getName()), 'newFile' => false)); $content = file_get_contents($servicesPath); if (!preg_match('/' . preg_quote('app.twig.' . strtolower($name) . '_extension', '/') . '/', $content)) { $this->writeFile($servicesPath, $servicesData, FILE_APPEND); $output->writeLn(' definition <info>added</info>'); } else { $output->writeLn(' definition <info>already exists</info>'); } } else { $output->writeLn(sprintf(' file <info>%s</info> does not exist.', $servicesPath)); $servicesData = $twig->render('services.yml.twig', array('name' => $name, 'fqcn' => $fqcn, 'bundle_alias' => $bundle->getName(), 'newFile' => true)); if ($dialog->askConfirmation($output, ' do you want me to create it? [Y/n] ', 'y')) { $this->writeFile($servicesPath, $servicesData); $output->writeLn(' definition <info>added</info>'); } else { $output->writeLn(sprintf(<<<EOT <info>The service definition was not written. You can manually create it:</info> %s EOT , $servicesData)); } } }
/** * Add an annotation controller resource. * * @param string $bundle * @param string $controller * * @return bool */ public function addRESTController($bundle, $controller) { $current = ''; if (file_exists($this->file)) { $current = file_get_contents($this->file); } elseif (!is_dir($dir = dirname($this->file))) { mkdir($dir, 0777, true); } $code = sprintf("%s:\n", Container::underscore(substr($bundle, 0, -6)) . '_' . Container::underscore($controller)); $code .= sprintf(" resource: \"%s\\Controller\\%sController\"\n type: rest\n", $bundle, $controller); $code .= "\n"; $code .= $current; return false !== file_put_contents($this->file, $code); }
/** * Adds an Admin service within the admin config file. * * @param BundleInterface $bundle * @param string $entity * @param string $group * @param string $label * @param string $translationDomain * * @return Boolean true if it worked, false otherwise * * @throws \RuntimeException If bundle is already imported */ public function addResource(BundleInterface $bundle, $entity, $group, $label, $translationDomain = 'Sonata') { $adminService = sprintf('%s.admin.%s', Container::underscore(substr($bundle->getName(), 0, -6)), strtolower($entity)); $adminClass = sprintf('%s\\Admin\\%sAdmin', $bundle->getNamespace(), ucfirst($entity)); $entityClass = sprintf('%s\\Entity\\%s', $bundle->getNamespace(), ucfirst($entity)); $content = Yaml::parse(file_get_contents($this->file)); $content['parameters'][$adminService . '.class'] = $adminClass; $content['services'][$adminService] = array('class' => "%{$adminService}.class%", 'tags' => array(array('name' => 'sonata.admin', 'manager_type' => 'orm', 'group' => $group, 'label' => $label)), 'arguments' => array(null, $entityClass, null), 'calls' => array(array('setTranslationDomain', array($translationDomain)))); $yaml = Yaml::dump($content, 4); if (false === file_put_contents($this->file, $yaml)) { return false; } return true; }
/** * Processes awareness interfaces by adding a setter method call to the service definition with a reference to the service. * * @param ContainerBuilder $container */ public function process(ContainerBuilder $container) { foreach ($container->getDefinitions() as $definition) { $interfaces = class_implements($definition->getClass()); foreach ($interfaces as $interface) { if (substr($interface, -14) === 'AwareInterface') { $camelCasedServiceId = substr($interface, strrpos($interface, '\\') + 1, -14); $serviceId = Container::underscore($camelCasedServiceId); if ($container->has($serviceId)) { $setterMethod = 'set' . $camelCasedServiceId; $definition->addMethodCall($setterMethod, array(new Reference($serviceId))); } } } } }
/** * @param string $namespace The namespace * @param string $bundle The bundle name * @param string $dir The directory * * @throws \RuntimeException */ public function generate($namespace, $bundle, $dir) { $dir .= '/' . strtr($namespace, '\\', '/'); if (file_exists($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not empty.', realpath($dir))); } $basename = substr($bundle, 0, -6); $parameters = array('namespace' => $namespace, 'bundle' => $bundle, 'bundle_basename' => $basename, 'extension_alias' => Container::underscore($basename)); $this->renderFile('/bundle/Bundle.php', $dir . '/' . $bundle . '.php', $parameters); $this->renderFile('/bundle/Extension.php', $dir . '/DependencyInjection/' . $basename . 'Extension.php', $parameters); $this->renderFile('/bundle/Configuration.php', $dir . '/DependencyInjection/Configuration.php', $parameters); $this->renderFile('/bundle/DefaultController.php', $dir . '/Controller/DefaultController.php', $parameters); $this->renderFile('/bundle/index.html.twig', $dir . '/Resources/views/Default/index.html.twig', $parameters); $this->renderFile('/bundle/services.yml', $dir . '/Resources/config/services.yml', $parameters); $this->renderFile('/bundle/routing.yml', $dir . '/Resources/config/routing.yml', $parameters); }
/** * @param BundleInterface $bundle * @param string $module * @param string $entity */ public function generate(BundleInterface $bundle, $module, $entity) { $parameters = array('namespace' => $bundle->getNamespace(), 'bundle' => $bundle->getName(), 'module' => $module, 'entity' => $entity, 'identifier' => Container::underscore($module), 'bundle_alias' => Container::underscore($bundle->getName())); $dir = $bundle->getPath(); $moduleFile = $dir . '/Module/' . $module . 'Module.php'; if (file_exists($moduleFile)) { throw new \RuntimeException(sprintf('Module "%s" already exists', $module)); } $formExtensionFile = $dir . '/Form/Module/' . $module . 'FormExtension.php'; if (file_exists($formExtensionFile)) { throw new \RuntimeException(sprintf('FormExtension "%s" already exists', $module)); } $this->renderFile('module/Module.php.twig', $moduleFile, $parameters); $this->renderFile('module/FormExtension.php.twig', $formExtensionFile, $parameters); $this->updateDependencyInjection($bundle, $parameters); }