src/Trinity/BlogBundle/Routing/ExtraLoader.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Trinity\BlogBundle\Routing;
  3. use Symfony\Component\Config\Loader\Loader;
  4. use Symfony\Component\Routing\Route;
  5. use Symfony\Component\Routing\RouteCollection;
  6. class ExtraLoader extends Loader
  7. {
  8.     /**
  9.      *
  10.      * @var \Doctrine\ORM\EntityManager
  11.      */
  12.     protected $em;
  13.     /**
  14.      * Constructor.
  15.      *
  16.      * @param \Doctrine\ORM\EntityManager $em the Doctrine Entity Manager
  17.      */
  18.     public function __construct(\Doctrine\ORM\EntityManager $em)
  19.     {
  20.         $this->em $em;
  21.     }
  22.     public function load($resource$type null) { return $collection; }
  23.     public function init($collection){
  24.         // $collection = $this->specialRouting(null, $collection, []);
  25.         // $this->initLabels();
  26.         // die();
  27.         return $collection;
  28.     }
  29.     public function specialRouting($parent null$collection$path = []){
  30.         $entries $this->em->createQuery('SELECT E FROM TrinityBlogBundle:Entry E ORDER BY E.datePublish desc')->execute();
  31.         foreach ($entries as $Entry){
  32.             $slug '/' $Entry->getId() . '/' $Entry->getDefaultSlug();
  33.             if(!empty($Entry->getSlug())){
  34.                 $slug '/' $Entry->getSlug();
  35.             }
  36.             $slugKey 'blog-redirect-' $Entry->getId();
  37.             $Route = new Route($slug '/{param1}/{param2}/{param3}');
  38.             $Route->setDefaults(array(
  39.                 '_controller' => 'App\\Trinity\\BlogBundle\\Controller\\BlogController::wpredirectAction',
  40.                 'entryId' => $Entry->getId(),
  41.                 'param1' => $Entry->getId(), 'param2' => null'param3' => null,
  42.             ));
  43.             $Route->setRequirements(array('param1' => '.*?''param2' => '.*?''param3' => '.*?'));
  44.             $Route->setMethods(array('POST''GET'));
  45.             $collection->add($slugKey$Route);
  46.         }
  47.         return $collection;
  48.     }
  49.     public function initLabels(){
  50.         $blogs $this->em->createQuery('SELECT B FROM TrinityBlogBundle:Blog B ORDER BY B.id ASC')->execute();
  51.         $cacheDir preg_replace('/\/src.*/''/var/cache/prod/'__DIR__);
  52.         $cacheFile $cacheDir 'titles_blog';
  53.         if (!file_exists($cacheDir)) {
  54.             mkdir($cacheDir);
  55.         }
  56.         // Clear/create file
  57.         file_put_contents($cacheFile'');
  58.         // Store format:
  59.         // locale    path    title
  60.         foreach($blogs as $Blog){
  61.             $locale $Blog->getLanguage()->getLocale();
  62.             foreach($Blog->getEntries() as $Entry){
  63.                 $detailUrl '/' $Entry->getId() . '/' $Entry->getDefaultSlug();
  64.                 file_put_contents($cacheFile$locale "\t" $detailUrl "\t" $Entry->getLabel() . "\n"FILE_APPEND);
  65.                 if($Entry->getSlug()){
  66.                     $detailUrl '/' $Entry->getSlug();
  67.                     file_put_contents($cacheFile$locale "\t" $detailUrl "\t" $Entry->getLabel() . "\n"FILE_APPEND);
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     public function supports($resource$type null) { return 'extra' === $type; }
  73. }