|
19 | 19 |
|
20 | 20 | final class ReactPhpBundleLoader extends Loader |
21 | 21 | { |
| 22 | + private array $assetPaths; |
| 23 | + private string $publicPath; |
| 24 | + |
22 | 25 | public function supports($resource, $type = null): bool |
23 | 26 | { |
24 | 27 | return 'reactphp_bundle' === $type; |
25 | 28 | } |
26 | 29 |
|
| 30 | + public function setProjectDir(string $path): void |
| 31 | + { |
| 32 | + $this->publicPath = rtrim($path, '/') . '/public'; |
| 33 | + } |
| 34 | + |
| 35 | + public function setAssetPaths(array $paths): void |
| 36 | + { |
| 37 | + $this->assetPaths = $paths; |
| 38 | + } |
| 39 | + |
27 | 40 | public function load(mixed $resource, ?string $type = null): RouteCollection |
28 | 41 | { |
29 | 42 | $routes = new RouteCollection(); |
30 | | - $routes->add( |
31 | | - name: 'zolex:reactphp_bundle', |
32 | | - route: new Route( |
33 | | - path: '/bundles/{file}', |
34 | | - defaults: [ |
35 | | - '_controller' => 'zolex.reactphp_bundle.serve_bundle_assets_action', |
36 | | - ], |
37 | | - requirements: ['file' => '.*'], |
38 | | - methods: ['GET'] |
39 | | - ), |
40 | | - ); |
| 43 | + foreach ($this->assetPaths as $assetPath) { |
| 44 | + $target = trim($assetPath, '/'); |
| 45 | + if (!is_readable($this->publicPath . '/'. $target)) { |
| 46 | + user_error('Tried to register "'. $target . '" asset path, but it does not exist or is not readable.', E_USER_WARNING); |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + if (is_file($this->publicPath . '/'. $target)) { |
| 51 | + $routes->add( |
| 52 | + name: 'zolex:reactphp_bundle:' . str_replace('/', '_', $target), |
| 53 | + route: new Route( |
| 54 | + path: '/' . $target, |
| 55 | + defaults: [ |
| 56 | + '_controller' => 'zolex.reactphp_bundle.serve_bundle_assets_action', |
| 57 | + 'directory' => '/', |
| 58 | + 'file' => $target, |
| 59 | + ], |
| 60 | + methods: ['GET'] |
| 61 | + ), |
| 62 | + ); |
| 63 | + } else { |
| 64 | + $routes->add( |
| 65 | + name: 'zolex:reactphp_bundle:' . str_replace('/', '_', $target), |
| 66 | + route: new Route( |
| 67 | + path: '/' . $target . '/{file}', |
| 68 | + defaults: [ |
| 69 | + '_controller' => 'zolex.reactphp_bundle.serve_bundle_assets_action', |
| 70 | + 'directory' => $target, |
| 71 | + ], |
| 72 | + requirements: ['file' => '.*'], |
| 73 | + methods: ['GET'] |
| 74 | + ), |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
41 | 78 |
|
42 | 79 | return $routes; |
43 | 80 | } |
|
0 commit comments