- Handles the @Route annotation at class and method level
This commit is contained in:
		
						commit
						9a9e01e1fe
					
				
							
								
								
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | ||||
| The MIT License (MIT) | ||||
| 
 | ||||
| Copyright (c) 2019 Dave Mc Nicoll | ||||
| 
 | ||||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
| of this software and associated documentation files (the "Software"), to deal | ||||
| in the Software without restriction, including without limitation the rights | ||||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
| copies of the Software, and to permit persons to whom the Software is | ||||
| furnished to do so, subject to the following conditions: | ||||
| 
 | ||||
| The above copyright notice and this permission notice shall be included in all | ||||
| copies or substantial portions of the Software. | ||||
| 
 | ||||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
| SOFTWARE. | ||||
							
								
								
									
										26
									
								
								composer.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								composer.json
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | ||||
| { | ||||
|     "name": "mcnd/notes-route", | ||||
|     "description": "Create a route map from annotations of given classes.", | ||||
|     "type": "library", | ||||
|     "license": "MIT", | ||||
|     "authors": [ | ||||
|         { | ||||
|             "name": "Dave Mc Nicoll", | ||||
|             "email": "mcndave@gmail.com" | ||||
|         } | ||||
|     ], | ||||
|     "require": { | ||||
|         "mcnd/notes": "master-dev", | ||||
|     }, | ||||
|     "repositories": [ | ||||
|         { | ||||
|             "type": "vcs", | ||||
|             "url": "https://github.com/mcNdave/notes.git" | ||||
|         } | ||||
|     ], | ||||
|     "autoload": { | ||||
|         "psr-4": { | ||||
|             "Notes\\Route\\": "src/" | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										33
									
								
								src/Annotation/Method/Route.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/Annotation/Method/Route.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,33 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Notes\Route\Annotation\Method; | ||||
| 
 | ||||
| class Route implements \Notes\Annotation { | ||||
| 
 | ||||
|     public string $name; | ||||
| 
 | ||||
|     public string $route; | ||||
| 
 | ||||
|     public string $method; | ||||
| 
 | ||||
|     public array $methods; | ||||
| 
 | ||||
|     public string $class; | ||||
| 
 | ||||
|     public string $classMethod; | ||||
| 
 | ||||
|     public function __construct($route = null, $name = null, $method = null) | ||||
|     { | ||||
|         if ( $route !== null ) { | ||||
|             $this->route = $route; | ||||
|         } | ||||
| 
 | ||||
|         if ( $name !== null ) { | ||||
|             $this->name = $name; | ||||
|         } | ||||
| 
 | ||||
|         if ( $method !== null ) { | ||||
|             $this->method = $method; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										15
									
								
								src/Annotation/Object/Route.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/Annotation/Object/Route.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Notes\Route\Annotation\Object; | ||||
| 
 | ||||
| class Route implements \Notes\Annotation { | ||||
| 
 | ||||
|     public array $methods; | ||||
| 
 | ||||
|     public function __construct(?array $methods = null) | ||||
|     { | ||||
|         if ( $methods !== null ) { | ||||
|             $this->methods = $methods; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										102
									
								
								src/RouteFetcher.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										102
									
								
								src/RouteFetcher.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,102 @@ | ||||
| <?php namespace Notes\Route; | ||||
| 
 | ||||
| use Notes\ObjectReflection, | ||||
|     Notes\ObjectResolver; | ||||
| 
 | ||||
| use RuntimeException, DirectoryIterator, Generator, Closure; | ||||
| 
 | ||||
| class RouteFetcher { | ||||
| 
 | ||||
|     protected array $folderList; | ||||
| 
 | ||||
|     protected Closure $callback; | ||||
| 
 | ||||
|     public array $defaultMethods = [ 'GET', 'POST' ]; | ||||
| 
 | ||||
|     protected array $annotations; | ||||
| 
 | ||||
|     public function __construct(?Closure $callback = null, ?array $folderList = null, ?array $annotations = null) | ||||
|     { | ||||
|         if ($callback !== null) { | ||||
|             $this->callback = $callback; | ||||
|         } | ||||
| 
 | ||||
|         if ($folderList !== null) { | ||||
|             $this->folderList = $folderList; | ||||
|         } | ||||
| 
 | ||||
|         if ($annotations !== null) { | ||||
|             $this->annotations = $annotations; | ||||
|         } | ||||
|         else { | ||||
|             $this->annotations = [ | ||||
|                 'object' => Annotation\Object\Route::class, | ||||
|                 'method' => Annotation\Method\Route::class, | ||||
|             ]; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public function addFolder($folder) : void | ||||
|     { | ||||
|         $this->folderList[] = $folder; | ||||
|     } | ||||
| 
 | ||||
|     public function setFolderList(array $list) : void | ||||
|     { | ||||
|         $this->folderList = $list; | ||||
|     } | ||||
| 
 | ||||
|     public function scan() : Generator | ||||
|     { | ||||
|         foreach($this->folderList as $namespace => $folder) { | ||||
|             if ( ! file_exists($folder) ) { | ||||
|                 throw new RuntimeException(sprintf("Folder `%s` can not be found or scanned", $folder)); | ||||
|             } | ||||
| 
 | ||||
|             foreach (new DirectoryIterator($folder) as $fileinfo) { | ||||
|                 if ( ! $fileinfo->isDot() ) { | ||||
|                     yield $namespace => $fileinfo; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public function compile() : Generator | ||||
|     { | ||||
|         foreach($this->scan() as $namespace => $file) { | ||||
|             $class = $this->generateClassname($file->getBasename(".php"), $namespace); | ||||
|             $methods = $this->defaultMethods; | ||||
| 
 | ||||
|             # Should generate an equivalent of Ulmus's object reflection here !
 | ||||
|             $objectResolver = new ObjectResolver($class, true, true, false, true); | ||||
| 
 | ||||
|             if ( null !== ( $object = $objectResolver->getAnnotationFromClassname( $this->annotations['object'] ) ) ) { | ||||
|                 if ( $object->methods ?? false ) { | ||||
|                     $methods = $object->methods; | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             $routeList = $objectResolver->getAnnotationListFromClassname( $this->annotations['method'] ); | ||||
| 
 | ||||
|             foreach($routeList as $func => $route) { | ||||
|                 $route->class = $class; | ||||
|                 $route->classMethod = $func; | ||||
| 
 | ||||
|                 if ( false === ( $route->methods ?? false ) ) { | ||||
|                     $route->methods = $methods; | ||||
|                 } | ||||
| 
 | ||||
|                 if ( false !== ( $this->callback ?? false ) ) { | ||||
|                     call_user_func_array($this->callback, [ $route ]); | ||||
|                 } | ||||
| 
 | ||||
|                 yield $route; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     protected function generateClassname($file, $namespace) | ||||
|     { | ||||
|         return "\\$namespace\\$file"; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user