Created
October 1, 2014 09:59
-
-
Save ilikeprograms/89d0e74731b7a15d0419 to your computer and use it in GitHub Desktop.
Loading Annotations/Route Collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // src/Corvus/CoreBundle/Extension/PortfolioInfoRepository.php | |
| namespace Corvus\CoreBundle\Extension; | |
| use Doctrine\Bundle\DoctrineBundle\Registry, | |
| Doctrine\Common\Annotations\FileCacheReader, | |
| Symfony\Component\Routing\Matcher\UrlMatcher, | |
| Symfony\Component\Routing\RequestContext, | |
| Symfony\Component\Routing\RouteCollection, | |
| Symfony\Component\Routing\Route, | |
| Symfony\Component\Routing\Router, | |
| Symfony\Component\Routing\Exception\ResourceNotFoundException, | |
| Symfony\Component\Routing\Exception\RouteNotFoundException, | |
| Symfony\Component\HttpFoundation\RequestStack; | |
| class PortfolioInfoRepository | |
| { | |
| .. Other methods :) | |
| /** | |
| * Loads the FrontendBundle's Controller Annotations and creates a RouteCollection. | |
| * | |
| * @return RouteCollection | |
| */ | |
| public function loadRouteCollection() | |
| { | |
| $collection = new RouteCollection(); | |
| // Itterate over all of the files/directories in the Frontend Bundle Controller folder | |
| foreach (new \DirectoryIterator(__DIR__.'/../../FrontendBundle/Controller') as $fileInfo) { | |
| // Skip . and .. and make sure we are Reflecting over files | |
| if($fileInfo->isDot() || !$fileInfo->isFile()) { continue; } | |
| // Store the full path classname | |
| $reflectionClassName = sprintf('\Corvus\FrontendBundle\Controller\%s', substr($fileInfo->getFilename(), 0, -4)); | |
| // Create a ReflectionClass with the class path, this is what the FileCacheReader public methods accept | |
| $reflectionClass = new \ReflectionClass($reflectionClassName); | |
| $classAnnotations = $this->annotationReader->getClassAnnotations($reflectionClass); | |
| $methodAnnotations = array(); | |
| $classPath = ''; | |
| // Find if there is a @Route annotation on the Controller class (i.e. Route prefix) | |
| foreach ($classAnnotations as $classAnnotation) { | |
| // We only need to process @Route annotations | |
| if (is_a($classAnnotation, 'Sensio\Bundle\FrameworkExtraBundle\Configuration\Route')) { | |
| // Get the @Route path | |
| $classPath = $classAnnotation->getPath(); | |
| } | |
| } | |
| // Itterate over each method in the ReflectionClass, i.e. Public Methods/Actions of the Controller | |
| foreach ($reflectionClass->getMethods() as $reflectionMethod) { | |
| array_push($methodAnnotations, $this->annotationReader->getMethodAnnotations($reflectionMethod)); | |
| } | |
| foreach ($methodAnnotations as $annotationTypes) { | |
| foreach ($annotationTypes as $annotationType) { | |
| // We only need to process @Route annotations | |
| if (is_a($annotationType, 'Sensio\Bundle\FrameworkExtraBundle\Configuration\Route')) { | |
| // Construct a Route from the Annotation information | |
| $route2 = new Route($classPath . $annotationType->getPattern(), $annotationType->getDefaults(), $annotationType->getRequirements()); | |
| // Add the Route to the Route Collection | |
| $collection->add($annotationType->getName(), $route2); | |
| } | |
| } | |
| } | |
| } | |
| return $collection; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment