Author: Fabien Potencier (fabien@symfony.com)
Inheritance: extends Sensio\Bundle\GeneratorBundle\Manipulator\Manipulator
Esempio n. 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var Kernel $kernel */
     $kernel = $this->getContainer()->get('kernel');
     $components = $this->getContainer()->getParameter('components');
     $kernelManipulator = new KernelManipulator($kernel);
     foreach ($components as $component) {
         $classArr = explode('\\', $component['class']);
         $bundleName = $classArr[0] . $classArr[1];
         $bundleClassName = $classArr[0] . '\\' . $classArr[1] . '\\' . $bundleName;
         if (!class_exists($bundleClassName)) {
             $bundleName = $classArr[0] . $classArr[1] . $classArr[2];
             $bundleClassName = $classArr[0] . '\\' . $classArr[1] . '\\' . $classArr[2] . '\\' . $bundleName;
             if (!class_exists($bundleClassName)) {
                 throw new \Exception("Bundle class not found");
             }
         }
         echo "Checking if " . $bundleName . " is in Kernel" . PHP_EOL;
         try {
             $kernel->getBundle($bundleName);
         } catch (\InvalidArgumentException $e) {
             // register bundle to kernel
             echo "Registering bundle " . $bundleName . PHP_EOL;
             try {
                 $kernelManipulator->addBundle($bundleClassName);
             } catch (\RuntimeException $e) {
                 return array(sprintf('Bundle <comment>%s</comment> is already defined in <comment>AppKernel::registerBundles()</comment>.', $bundleClassName), '');
             }
             $output->writeln($bundleName . " succesfully registered to Kernel." . PHP_EOL);
         }
     }
 }
 protected function updateKernel(OutputInterface $output, KernelInterface $kernel, Bundle $bundle)
 {
     $kernelManipulator = new KernelManipulator($kernel);
     $output->write(sprintf('> Enabling the bundle inside <info>%s</info>: ', $this->makePathRelative($kernelManipulator->getFilename())));
     try {
         $ret = $kernelManipulator->addBundle($bundle->getBundleClassName());
         if (!$ret) {
             $reflected = new \ReflectionObject($kernel);
             return array(sprintf('- Edit <comment>%s</comment>', $reflected->getFilename()), '  and add the following bundle in the <comment>AppKernel::registerBundles()</comment> method:', '', sprintf('    <comment>new %s(),</comment>', $bundle->getBundleClassName()), '');
         }
     } catch (\RuntimeException $e) {
         return array(sprintf('Bundle <comment>%s</comment> is already defined in <comment>AppKernel::registerBundles()</comment>.', $bundle->getBundleClassName()), '');
     }
 }
 /**
  * @param string[] $arrBundles  array of full class names, all with namespace, of the bundles to be added to the kernel
  * @param KernelInterface $kernel
  * @param OutputInterface $output
  * @param string[] $alreadyAdded
  * @return bool
  */
 protected function updateBundles(array $arrBundles, KernelInterface $kernel, OutputInterface $output, &$alreadyAdded)
 {
     $manipulator = new KernelManipulator($kernel);
     $alreadyAdded = array();
     $result = true;
     foreach ($arrBundles as $bundle) {
         try {
             $ok = $manipulator->addBundle($bundle);
             $result = $result && $ok;
         } catch (\RuntimeException $ex) {
             $alreadyAdded[] = $bundle;
         }
     }
     return $result;
 }
 protected function updateKernel(InputInterface $input, OutputInterface $output, KernelInterface $kernel, $bundle)
 {
     $manip = new KernelManipulator($kernel);
     try {
         $ret = $manip->addBundle($bundle);
         if (!$ret) {
             $reflected = new \ReflectionObject($kernel);
             $output->writeln(sprintf('- Edit <comment>%s</comment>', $reflected->getFilename()));
             $output->writeln("  and add the following bundle in the <comment>AppKernel::registerBundles()</comment> method:\n");
             $output->writeln(sprintf("    <comment>new %s(),</comment>\n", $bundle));
         }
     } catch (\RuntimeException $e) {
         $output->writeln(sprintf('Bundle <comment>%s</comment> is already defined in <comment>AppKernel::registerBundles()</comment>.', $bundle));
         throw $e;
     }
 }
 /**
  * @dataProvider kernelStubFilenamesProvider
  *
  * @param string $kernelOriginFilePath
  */
 public function testAddToArray($kernelOriginFilePath)
 {
     if (defined('HHVM_VERSION')) {
         $this->markTestSkipped('Not supported in HHVM since it doesn\'t allow to lint PHP files.');
     }
     $params = $this->prepareTestKernel($kernelOriginFilePath);
     list($kernelClassName, $fullpath) = $params;
     $kernelClassName = self::STUB_NAMESPACE . '\\' . $kernelClassName;
     $this->registerClassLoader($kernelClassName, $fullpath);
     $kernel = new $kernelClassName('test', true);
     $manipulator = new KernelManipulator($kernel);
     $manipulator->addBundle(self::STUB_BUNDLE_CLASS_NAME);
     $phpFinder = new PhpExecutableFinder();
     $phpExecutable = $phpFinder->find();
     $this->assertNotSame(false, $phpExecutable, 'Php executable binary found');
     $pb = new ProcessBuilder();
     $process = $pb->add($phpExecutable)->add('-l')->add($fullpath)->getProcess();
     $process->run();
     $result = strpos($process->getOutput(), 'No syntax errors detected');
     $this->assertNotSame(false, $result, 'Manipulator should not provoke syntax errors');
 }
Esempio n. 6
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $name = $input->getArgument('name');
     //update Kernel
     $kernelManipulator = new KernelManipulator($this->getContainer()->get('kernel'));
     $nameArr = explode('/', $name);
     $bundle = array_pop($nameArr);
     $class = '';
     $namespace = '';
     foreach ($nameArr as $name) {
         $class .= $name;
         $namespace .= $name . '\\';
     }
     $class .= $bundle;
     try {
         $kernelManipulator->addBundle($namespace . $bundle . '\\' . $class);
     } catch (\RuntimeException $e) {
         return array(sprintf('Bundle <comment>%s</comment> is already defined in <comment>AppKernel::registerBundles()</comment>.', $namespace . '\\' . $bundle), '');
     }
     $output->writeln("Bundle succesfully registered to Kernel.");
 }
 protected function updateKernel(QuestionHelper $questionHelper, InputInterface $input, OutputInterface $output, KernelInterface $kernel, $namespace, $bundle)
 {
     $auto = true;
     if ($input->isInteractive()) {
         $question = new ConfirmationQuestion($questionHelper->getQuestion('Confirm automatic update of your Kernel', 'yes', '?'), true);
         $auto = $questionHelper->ask($input, $output, $question);
     }
     $output->write('Enabling the bundle inside the Kernel: ');
     $manip = new KernelManipulator($kernel);
     try {
         $ret = $auto ? $manip->addBundle($namespace . '\\' . $bundle) : false;
         if (!$ret) {
             $reflected = new \ReflectionObject($kernel);
             return array(sprintf('- Edit <comment>%s</comment>', $reflected->getFilename()), '  and add the following bundle in the <comment>AppKernel::registerBundles()</comment> method:', '', sprintf('    <comment>new %s(),</comment>', $namespace . '\\' . $bundle), '');
         }
     } catch (\RuntimeException $e) {
         return array(sprintf('Bundle <comment>%s</comment> is already defined in <comment>AppKernel::registerBundles()</comment>.', $namespace . '\\' . $bundle), '');
     }
 }
Esempio n. 8
0
 private function updateKernel(OutputInterface $output, KernelInterface $kernel, $namespace, $bundle)
 {
     $output->write('Enabling the bundle inside the Kernel: ');
     $kernel = new KernelManipulator($kernel);
     if (!$kernel->addBundle($namespace . '\\' . $bundle)) {
         $reflected = new \ReflectionObject($kernel);
         return array(sprintf('- Edit <comment>%s</comment>', $reflected->getFilename()), '  and add the following bundle in the <comment>AppKernel::registerBundles()</comment> method:', '', sprintf('      <comment>new %s(),</comment>', $namespace . '\\' . $bundle), '');
     }
 }
 /**
  * Update AppKernel class (small modifications over Fabien Potencier implementation found on GenerateBundleCommand)
  *
  * @param InputInterface  $input
  * @param OutputInterface $output
  * @param KernelInterface $kernel
  * @param                 $namespace
  * @param                 $bundle
  *
  * @return array
  */
 protected function updateKernel(InputInterface $input, OutputInterface $output, KernelInterface $kernel, $namespace, $bundle)
 {
     $auto = true;
     $output->write('Enabling the bundle inside the Kernel: ');
     $manip = new KernelManipulator($kernel);
     try {
         $ret = $auto ? $manip->addBundle($namespace . '\\' . $bundle) : false;
         if (!$ret) {
             $reflected = new \ReflectionObject($kernel);
             return array(sprintf('- Edit <comment>%s</comment>', $reflected->getFilename()), '  and add the following bundle in the <comment>AppKernel::registerBundles()</comment> method:', '', sprintf('    <comment>new %s(),</comment>', $namespace . '\\' . $bundle), '');
         }
     } catch (\RuntimeException $e) {
         return array(sprintf('Bundle <comment>%s</comment> is already defined in <comment>AppKernel::registerBundles()</comment>.', $namespace . '\\' . $bundle), '');
     }
 }
 protected function updateKernel($dialog, InputInterface $input, OutputInterface $output, KernelInterface $kernel, $namespace, $bundle)
 {
     $auto = true;
     if ($input->isInteractive()) {
         $auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic update of your Kernel', 'yes', '?'), true);
     }
     $output->write('Enabling the bundle inside the Kernel: ');
     $manip = new KernelManipulator($kernel);
     $ret = $auto ? $manip->addBundle($namespace . '\\' . $bundle) : false;
     if (!$ret) {
         $reflected = new \ReflectionObject($kernel);
         return array(sprintf('- Edit <comment>%s</comment>', $reflected->getFilename()), '  and add the following bundle in the <comment>AppKernel::registerBundles()</comment> method:', '', sprintf('    <comment>new %s(),</comment>', $namespace . '\\' . $bundle), '');
     }
 }
Esempio n. 11
0
 /**
  * Register bundle in Kernel
  * @param  KernelInterface $kernel
  * @param  sting           $namespace
  * @param  sting           $bundle
  * @return boolean
  * @throws RuntimeException         When bundle already defined in <comment>AppKernel::registerBundles()</comment>
  */
 public function registerBundle(KernelInterface $kernel, $namespace, $bundle)
 {
     $manip = new KernelManipulator($kernel);
     return $manip->addBundle($namespace . '\\' . $bundle);
 }
Esempio n. 12
0
 /**
  * Ajax action to install defined package.
  *
  * @Route("/install-bundle-ajax", name="guiInstallBundleAjax")
  * @Template()
  */
 public function installBundleAjaxAction()
 {
     $request = Request::createFromGlobals();
     $bundlePath = $request->request->get('bundlePath');
     $bundleVersion = $request->request->get('bundleVersion');
     $bundleName = $request->request->get('bundleName');
     $bundleTitle = $request->request->get('bundleTitle');
     $bundleNamespace = $request->get('bundleNamespace');
     $rootPath = rtrim(dirname($this->get('kernel')->getRootDir()), '/');
     $routingEntry = $request->request->get('routingEntry');
     $configuration = $request->request->get('configuration');
     if (!$bundlePath) {
         die('Error: Bundle path not set.');
     }
     if (!$bundleVersion) {
         die('Error: Bundle version not set.');
     }
     // insert bundle to composer.json file
     $composerJsonFile = $rootPath . '/composer.json';
     // use symfony file system
     $fs = new FS();
     if ($fs->exists($composerJsonFile)) {
         // read entries from file
         $composerFile = file_get_contents($composerJsonFile);
         // decode json string into object
         $composerJson = json_decode($composerFile);
         // check object if it a valid object
         if ($composerJson && is_object($composerJson)) {
             // retrieve all requirements from composer json object
             $composerRequires = $composerJson->require;
             // check if we have allready set the new bundle
             if (!isset($composerRequires->{$bundlePath})) {
                 // set new bundle and their version
                 $composerRequires->{$bundlePath} = $bundleVersion;
                 // override composer requirements with new one
                 $composerJson->require = $composerRequires;
                 // encode the json object
                 $data = json_encode($composerJson, JSON_PRETTY_PRINT);
                 // prepare json to a pretty json
                 $data = BundleUtil::getPrettyJson($data);
                 // dump json string into file
                 // mode 0664 = read/write for user and group and read for all other
                 file_put_contents($composerJsonFile, $data);
                 //$fs->dumpFile($composerJsonFile, '', 0777);
                 //$fs->dumpFile($composerJsonFile, $data, 0777);
             }
             unset($composerRequires);
         }
         unset($composerJson);
     }
     unset($composerJsonFile, $fs);
     /**
      * Anonymous callback for process
      *
      * @param string    $type   The process type
      * @param mixed     $output The output of process
      */
     $callback = function ($type, $output) {
         if (Process::ERR === $type) {
             echo 'error on executing composer: \\n\\n' . $output;
             die;
         }
     };
     // execute composer self-update
     $processBuilder = new ProcessBuilder($this->getComposerArguments($rootPath, 'self-update'));
     $processBuilder->setEnv('PATH', $request->server->get('PATH'));
     $processBuilder->setEnv('COMPOSER_HOME', $rootPath . '/bin');
     $process = $processBuilder->getProcess();
     $process->setTimeout(90);
     $process->setIdleTimeout(NULL);
     $ret = $process->run($callback);
     if ($ret == 0) {
         echo 'Execute composer self-update finished.';
     }
     unset($processBuilder, $process);
     // execute composer update on specified bundle
     $processBuilder = new ProcessBuilder($this->getComposerArguments($rootPath, 'update', $bundlePath));
     $processBuilder->setEnv('PATH', $request->server->get('PATH'));
     $processBuilder->setEnv('COMPOSER_HOME', $rootPath . '/bin');
     $processBuilder->setWorkingDirectory($rootPath);
     // Generate output for AJAX call
     echo 'Running update on: ' . $bundleTitle;
     $process = $processBuilder->getProcess();
     $process->setTimeout(3600);
     $process->setIdleTimeout(60);
     $ret = $process->run($callback);
     if ($ret == 0) {
         // Register new bundle after it was installed
         $kernel = $this->get('kernel');
         if ($kernel instanceof Kernel) {
             // Check if bundle already installed
             $bundleInstalled = BundleUtil::bundleInstalled($this, $bundleTitle);
             if (!$bundleInstalled) {
                 // Register bundle
                 $km = new KernelManipulator($kernel);
                 try {
                     $km->addBundle(urldecode($bundleNamespace) . '\\' . $bundleName);
                 } catch (RuntimeException $ex) {
                     echo $ex->getMessage();
                     die;
                 }
                 unset($km);
             }
         }
         // Handle configuration at config.yml
         if (isset($configuration) && !empty($configuration)) {
             $this->_addConfiguration($bundleTitle, $rootPath, $configuration);
         }
         // Handle route installation
         if (isset($routingEntry) && !empty($routingEntry)) {
             $this->_addRouting($bundleTitle, $rootPath, $routingEntry);
         }
         // Clear cache
         BundleUtil::clearCache($kernel);
         // handle success
         echo 'Done';
     } else {
         // handle error
         echo 'Error on updating: ' . $bundleTitle . '\\n\\n' . Process::$exitCodes[$ret];
     }
     unset($processBuilder, $process);
     exit;
 }