public function testRenderDir() { Mustache::renderDir($this->dir, array('me' => 'Fabien')); $this->assertEquals('Hello Fabien', file_get_contents($this->dir.'/template.txt'), '::renderDir() renders a directory'); $this->assertEquals('Hello Fabien', file_get_contents($this->dir.'/foo/bar.txt'), '::renderDir() renders a directory'); }
/** * @see Command * * @throws \InvalidArgumentException When namespace doesn't end with Bundle * @throws \RuntimeException When bundle can't be executed */ protected function execute(InputInterface $input, OutputInterface $output) { if (!preg_match('/Bundle$/', $namespace = $input->getArgument('namespace'))) { throw new \InvalidArgumentException('The namespace must end with Bundle.'); } // validate namespace if (preg_match('/[^A-Za-z0-9_\\\\-]/', $namespace)) { throw new \InvalidArgumentException('The namespace contains invalid characters.'); } // user specified bundle name? $bundle = $input->getArgument('bundleName'); if (!$bundle) { $bundle = strtr($namespace, array('\\' => '')); } if (!preg_match('/Bundle$/', $bundle)) { throw new \InvalidArgumentException('The bundle name must end with Bundle.'); } $dir = $input->getArgument('dir'); // add trailing / if necessary $dir = '/' === substr($dir, -1, 1) ? $dir : $dir . '/'; $targetDir = $dir . strtr($namespace, '\\', '/'); $output->writeln(sprintf('Initializing bundle "<info>%s</info>" in "<info>%s</info>"', $bundle, $dir)); if (file_exists($targetDir)) { throw new \RuntimeException(sprintf('Bundle "%s" already exists.', $bundle)); } $filesystem = $this->container->get('filesystem'); $filesystem->mirror(__DIR__ . '/../Resources/skeleton/bundle/generic', $targetDir); $filesystem->mirror(__DIR__ . '/../Resources/skeleton/bundle/' . $input->getOption('format'), $targetDir); Mustache::renderDir($targetDir, array('namespace' => $namespace, 'bundle' => $bundle)); rename($targetDir . '/Bundle.php', $targetDir . '/' . $bundle . '.php'); }
protected function execute(InputInterface $input, OutputInterface $output) { // validate bundle if (!preg_match('/Bundle$/', $bundle = $input->getArgument('bundle'))) { throw new \InvalidArgumentException('The bundle must end with Bundle'); } // validate that the namespace is at least one level deep if (false === strpos($bundle, '\\')) { throw new \InvalidArgumentException('The bundle must contain the vendor with quotes, exemple "vendors\\MyBundle"'); } if (!is_dir($bundleDir = 'src/' . strtr($bundle, '\\', '/'))) { throw new \RuntimeException(sprintf('The directory %s doesn\'t exists, are you sure that it is the correct bundle ?', $bundleDir)); } // validate namespace $namespace = $bundle . '\\Tests'; $path = $input->getArgument('path'); if (isset($path)) { $namespace .= '\\' . $path; } $namespace = strtr($namespace, '/', '\\'); if (preg_match('/[^A-Za-z0-9_\\\\-]/', $namespace)) { throw new \InvalidArgumentException('The namespace contains invalid characters.'); } if (!preg_match('/Test$/', $filename = $input->getArgument('filename'))) { throw new \InvalidArgumentException('The filename musts end with Test'); } $targetFile = 'src/' . strtr($bundle, '\\', '/') . '/Tests/' . $path . '/' . $filename . '.php'; if (file_exists($targetFile)) { throw new \RuntimeException(sprintf('The "%s" test file already exists.', $targetFile)); } $filesystem = $this->container->get('filesystem'); $filesystem->copy(__DIR__ . '/../Resources/skeleton/UnitTestCase.php', $targetFile); Mustache::renderFile($targetFile, array('namespace' => $namespace, 'className' => $filename)); $output->writeln('<info>[File+]</info> ' . $targetFile); }
/** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $finder = new Finder(); if (iterator_count($finder->in(getcwd()))) { throw new \RuntimeException('The current directory is not empty.'); } $parameters = array('class' => $input->getOption('name'), 'application' => strtolower($input->getOption('name')), 'format' => $input->getOption('format')); $filesystem = new Filesystem(); $skeletonDir = __DIR__ . '/../../skeleton'; $appPath = getcwd() . '/' . $input->getOption('app-path'); $srcPath = getcwd() . '/' . $input->getOption('src-path'); $webPath = getcwd() . '/' . $input->getOption('web-path'); $filesystem->mirror($skeletonDir . '/application/generic', $appPath); $filesystem->mirror($skeletonDir . '/application/' . $input->getOption('format'), $appPath); $filesystem->mirror($skeletonDir . '/src', $srcPath); Mustache::renderDir($appPath, $parameters); $filesystem->chmod($appPath . '/console', 0777); $filesystem->chmod($appPath . '/logs', 0777); $filesystem->chmod($appPath . '/cache', 0777); $filesystem->rename($appPath . '/Kernel.php', $appPath . '/' . $input->getOption('name') . 'Kernel.php'); $filesystem->rename($appPath . '/Cache.php', $appPath . '/' . $input->getOption('name') . 'Cache.php'); $filesystem->copy($skeletonDir . '/web/front_controller.php', $file = $webPath . '/' . (file_exists($webPath . '/index.php') ? strtolower($input->getOption('name')) : 'index') . '.php'); Mustache::renderFile($file, $parameters); $filesystem->copy($skeletonDir . '/web/front_controller_debug.php', $file = $webPath . '/' . strtolower($input->getOption('name')) . '_dev.php'); Mustache::renderFile($file, $parameters); }
/** * @see Command * * @throws \InvalidArgumentException When namespace doesn't end with Bundle * @throws \RuntimeException When bundle can't be executed */ protected function execute(InputInterface $input, OutputInterface $output) { if (!preg_match('/Bundle$/', $namespace = $input->getArgument('namespace'))) { throw new \InvalidArgumentException('The namespace must end with Bundle.'); } // validate namespace if (preg_match('/[^A-Za-z0-9_\\\\-]/', $namespace)) { throw new \InvalidArgumentException('The namespace contains invalid characters.'); } // user specified bundle name? $bundle = $input->getArgument('bundleName'); if (!$bundle) { $bundle = strtr($namespace, array('\\' => '')); } if (!preg_match('/Bundle$/', $bundle)) { throw new \InvalidArgumentException('The bundle name must end with Bundle.'); } // validate that the namespace is at least one level deep if (false === strpos($namespace, '\\')) { $msg = array(); $msg[] = sprintf('The namespace must contain a vendor namespace (e.g. "VendorName\\%s" instead of simply "%s").', $namespace, $namespace); $msg[] = 'If you\'ve specified a vendor namespace, did you forget to surround it with quotes (init:bundle "Acme\\BlogBundle")?'; throw new \InvalidArgumentException(implode("\n\n", $msg)); } $dir = $input->getArgument('dir'); // add trailing / if necessary $dir = '/' === substr($dir, -1, 1) ? $dir : $dir . '/'; $targetDir = $dir . strtr($namespace, '\\', '/'); if (file_exists($targetDir)) { throw new \RuntimeException(sprintf('Bundle "%s" already exists.', $bundle)); } $filesystem = $this->container->get('filesystem'); $filesystem->mirror(__DIR__ . '/../Resources/skeleton/bundle/generic', $targetDir); $filesystem->mirror(__DIR__ . '/../Resources/skeleton/bundle/' . $input->getOption('format'), $targetDir); Mustache::renderDir($targetDir, array('namespace' => $namespace, 'bundle' => $bundle)); rename($targetDir . '/Bundle.php', $targetDir . '/' . $bundle . '.php'); $output->writeln('<comment>Summary of actions</comment>'); $output->writeln(sprintf('- The bundle "<info>%s</info>" was created at "<info>%s</info>" and is using the namespace "<info>%s</info>".', $bundle, $targetDir, $namespace)); $output->writeln(sprintf('- The bundle contains a sample controller, a sample template and a sample routing file.')); $output->writeln(''); $output->writeln('<comment>Follow-up actions</comment>'); $output->writeln('- Enable the bundle inside the AppKernel::registerBundles() method.'); $output->writeln(' Resource: <info>http://symfony.com/doc/2.0/book/page_creation.html#create-the-bundle</info>'); $output->writeln('- Ensure that the namespace is registered with the autoloader.'); $output->writeln(' Resource: <info>http://symfony.com/doc/2.0/book/page_creation.html#autoloading-introduction-sidebar</info>'); $output->writeln('- If using routing, import the bundle\'s routing resource.'); $output->writeln(' Resource: <info>http://symfony.com/doc/2.0/book/routing.html#including-external-routing-resources</info>'); $output->writeln('- Starting building your bundle!'); $output->writeln(' Resource: <info>http://symfony.com/doc/2.0/book/page_creation.html#the-hello-symfony-page</info>'); }
/** * @see Command * * @throws \InvalidArgumentException When namespace doesn't end with Bundle * @throws \RuntimeException When bundle can't be executed */ protected function execute(InputInterface $input, OutputInterface $output) { if (!preg_match('/Bundle$/', $namespace = $input->getArgument('namespace'))) { throw new \InvalidArgumentException('The namespace must end with Bundle.'); } $dirs = $this->container->get('kernel')->getBundleDirs(); $tmp = str_replace('\\', '/', $namespace); $namespace = str_replace('/', '\\', dirname($tmp)); $bundle = basename($tmp); if (!isset($dirs[$namespace])) { throw new \InvalidArgumentException(sprintf("Unable to initialize the bundle (%s is not a defined namespace).\n" . "Defined namespaces are: %s", $namespace, implode(', ', array_keys($dirs)))); } $dir = $dirs[$namespace]; $output->writeln(sprintf('Initializing bundle "<info>%s</info>" in "<info>%s</info>"', $bundle, realpath($dir))); if (file_exists($targetDir = $dir . '/' . $bundle)) { throw new \RuntimeException(sprintf('Bundle "%s" already exists.', $bundle)); } $filesystem = new Filesystem(); $filesystem->mirror(__DIR__ . '/../Resources/skeleton/bundle', $targetDir); Mustache::renderDir($targetDir, array('namespace' => $namespace, 'bundle' => $bundle)); rename($targetDir . '/Bundle.php', $targetDir . '/' . $bundle . '.php'); }
/** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { if (file_exists($targetDir = $input->getArgument('path'))) { throw new \RuntimeException(sprintf('The directory "%s" already exists.', $targetDir)); } if (!file_exists($webDir = $input->getArgument('web_path'))) { mkdir($webDir, 0777, true); } $parameters = array('class' => $input->getArgument('name'), 'application' => strtolower($input->getArgument('name')), 'format' => $input->getOption('format')); $filesystem = new Filesystem(); $filesystem->mirror(__DIR__ . '/../Resources/skeleton/application/generic', $targetDir); $filesystem->mirror(__DIR__ . '/../Resources/skeleton/application/' . $input->getOption('format'), $targetDir); Mustache::renderDir($targetDir, $parameters); $filesystem->chmod($targetDir . '/console', 0777); $filesystem->chmod($targetDir . '/logs', 0777); $filesystem->chmod($targetDir . '/cache', 0777); $filesystem->rename($targetDir . '/Kernel.php', $targetDir . '/' . $input->getArgument('name') . 'Kernel.php'); $filesystem->rename($targetDir . '/Cache.php', $targetDir . '/' . $input->getArgument('name') . 'Cache.php'); $filesystem->copy(__DIR__ . '/../Resources/skeleton/web/front_controller.php', $file = $webDir . '/' . (file_exists($webDir . '/index.php') ? strtolower($input->getArgument('name')) : 'index') . '.php'); Mustache::renderFile($file, $parameters); $filesystem->copy(__DIR__ . '/../Resources/skeleton/web/front_controller_debug.php', $file = $webDir . '/' . strtolower($input->getArgument('name')) . '_dev.php'); Mustache::renderFile($file, $parameters); }
/** * Find and replace * * @param $input * @param $bundles * @param $registerNamespaces * @param $registerPrefixes */ private function findAndReplace($input, $bundles, $registerNamespaces, $registerPrefixes, $customConfig) { $twig_config = ('twig' === $input->getOption('template-engine')) ? $this->loadConfigFile('twig') : ''; $assetic_config = ($input->getOption('assetic')) ? $this->loadConfigFile('assetic') : ''; $assetic_dev_config = ($input->getOption('assetic')) ? $this->loadConfigFile('assetic_dev') : ''; $doctrine_config = ('doctrine' === $input->getOption('orm')) ? $this->loadConfigFile('doctrine') : ''; $doctrine_config = str_replace( array('{{ app }}', '{{ namespace }}'), array($input->getArgument('app'), $input->getArgument('vendor')), $doctrine_config); $propel_config = ('propel' === $input->getOption('orm')) ? $this->loadConfigFile('propel') : ''; $swift_config = ($input->getOption('swiftmailer')) ? $this->loadConfigFile('swiftmailer') : ''; $swift_autoload = ($input->getOption('swiftmailer')) ? $this->loadConfigFile('swiftmailer_autoload') : ''; $swift_test = ($input->getOption('swiftmailer')) ? $this->loadConfigFile('swiftmailer_test') : ''; $routing = ($input->getOption('controller')) ? $this->loadConfigFile('routing') : ''; $routing = str_replace( array('{{ app }}', '{{ namespace }}'), array($input->getArgument('app'), $input->getArgument('vendor')), $routing); $mandango_config = ('mandango' === $input->getOption('odm')) ? $this->loadConfigFile('mandango_config') : ''; $mandango_config_dev = ('mandango' === $input->getOption('odm')) ? $this->loadConfigFile('mandango_config_dev') : ''; Mustache::renderDir($input->getArgument('path'), array( 'namespace' => $input->getArgument('vendor'), 'appname' => $input->getArgument('app'), 'controller' => $input->getOption('controller'), 'secret' => Tool::generateSecret(), 'session_start' => $input->getOption('session-start') ? 'true' : 'false', 'session_name' => $input->getOption('session-name'), 'template_engine' => $input->getOption('template-engine'), 'routing' => $routing, 'bundles' => $bundles, 'registerNamespaces' => $registerNamespaces, 'registerPrefixes' => $registerPrefixes, 'twig' => $twig_config, 'assetic' => $assetic_config, 'assetic_dev' => $assetic_dev_config, 'doctrine' => $doctrine_config, 'propel' => $propel_config, 'swiftmailer' => $swift_config, 'swiftmailer_autoload' => $swift_autoload, 'swiftmailer_test' => $swift_test, 'custom' => $customConfig, 'mandango_config' => $mandango_config, 'mandango_config_dev' => $mandango_config_dev )); }