1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace ElementsFramework\DynamicRouting\Service\Compiler; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use ElementsFramework\DynamicRouting\Model\DynamicRoute; |
8
|
|
|
use Illuminate\Support\Facades\File; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class RouteDeclarationCompiler |
12
|
|
|
* @package ElementsFramework\DynamicRouting\Service\Compiler |
13
|
|
|
*/ |
14
|
|
|
class RouteDeclarationCompiler |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Returns a compiled definition for the route entry. |
19
|
|
|
* @param DynamicRoute $dynamicRoute |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public function compileRoute(DynamicRoute $dynamicRoute) |
23
|
|
|
{ |
24
|
|
|
$action = 'ElementsFramework\DynamicRouting\Service\RouteClosureProvider::forRouteId('.$dynamicRoute->id.')'; |
|
|
|
|
25
|
|
|
return 'Route::' . $dynamicRoute->method . '("'.$dynamicRoute->pattern.'", '.$action.')->name("'.$dynamicRoute->name.'");'; |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns the output for the whole dynamic routes file. |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function compileAllRoutes() |
33
|
|
|
{ |
34
|
|
|
$routes = DynamicRoute::all(); |
35
|
|
|
|
36
|
|
|
$output = ""; |
37
|
|
|
if(!empty(config('dynamic-routing.middleware-group'))) { |
38
|
|
|
$output .= "Route::group(['middleware' => '".config('dynamic-routing.middleware-group')."'], function () {" . PHP_EOL; |
39
|
|
|
} |
40
|
|
|
foreach($routes as $route) { |
41
|
|
|
$output .= $this->compileRoute($route) . PHP_EOL; |
42
|
|
|
} |
43
|
|
|
if(!empty(config('dynamic-routing.middleware-group'))) { |
44
|
|
|
$output .= "});" . PHP_EOL; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $output; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Saves the compiled routes to the file set in the configuration. |
52
|
|
|
*/ |
53
|
|
|
public function compileToFile() |
54
|
|
|
{ |
55
|
|
|
$contents = $this->compileAllRoutes(); |
56
|
|
|
$contents = "<?php" . PHP_EOL . $contents; |
57
|
|
|
File::put(base_path(config('dynamic-routing.compiled-routes-path')), $contents); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
} |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.