Knp\Bundle\SitemapBundle\Command\GenerateCommand::execute PHP Method

execute() protected method

protected execute ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$input Symfony\Component\Console\Input\InputInterface
$output Symfony\Component\Console\Output\OutputInterface
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getEntityManager($input->getOption('em'));
        $c = $this->getContainer();
        if (!$c->hasParameter('kb_sitemap.base_url')) {
            throw new \RuntimeException("Sitemap requires base_url parameter [kb_sitemap.base_url] to be available, through config or parameters");
        }
        $output->write('<info>Fetching resources..</info>' . PHP_EOL);
        $dql = <<<___SQL
        SELECT b.name, b.ownerName, b.updatedAt FROM KnpBundlesBundle:Bundle b
___SQL;
        $q = $em->createQuery($dql);
        $bundles = $q->getArrayResult();
        $dql = <<<___SQL
        SELECT d.name, d.createdAt FROM KnpBundlesBundle:Developer d
___SQL;
        $q = $em->createQuery($dql);
        $users = $q->getArrayResult();
        $sitemapFile = $c->getParameter('kernel.root_dir') . '/../web/sitemap.xml';
        $output->write('<info>Building sitemap...</info>' . PHP_EOL);
        $spaceless = (bool) $input->getOption('spaceless');
        $tpl = $spaceless ? 'KnpSitemapBundle::sitemap.spaceless.xml.twig' : 'KnpSitemapBundle::sitemap.xml.twig';
        $sitemap = $c->get('templating')->render($tpl, compact('bundles', 'users'));
        $output->write("<info>Saving sitemap in [{$sitemapFile}]..</info>" . PHP_EOL);
        file_put_contents($sitemapFile, $sitemap);
        // gzip the sitemap
        if (function_exists('gzopen')) {
            $output->write("<info>Gzipping the generated sitemap [{$sitemapFile}.gz]..</info>" . PHP_EOL);
            $gz = gzopen($sitemapFile . '.gz', 'w9');
            gzwrite($gz, $sitemap);
            gzclose($gz);
        }
        $output->write('<info>Done</info>' . PHP_EOL);
    }
GenerateCommand