| 1: | <?php |
| 2: | |
| 3: | namespace LaravelUi5\Core\Ui5; |
| 4: | |
| 5: | use LaravelUi5\Core\Attributes\Parameter; |
| 6: | use LaravelUi5\Core\Contracts\ParameterizableInterface; |
| 7: | use LaravelUi5\Core\Enums\ArtifactType; |
| 8: | use LaravelUi5\Core\Enums\ParameterSource; |
| 9: | use LaravelUi5\Core\Exceptions\InvalidHttpMethodActionException; |
| 10: | use LaravelUi5\Core\Exceptions\InvalidModuleException; |
| 11: | use LaravelUi5\Core\Exceptions\InvalidParameterSourceException; |
| 12: | use LaravelUi5\Core\Ui5\Capabilities\LaravelUi5ManifestInterface; |
| 13: | use LaravelUi5\Core\Ui5\Capabilities\LaravelUi5ManifestKeys; |
| 14: | use LaravelUi5\Core\Ui5\Capabilities\Ui5ShellFragmentInterface; |
| 15: | use LaravelUi5\Core\Ui5\Contracts\Ui5ModuleInterface; |
| 16: | use LaravelUi5\Core\Ui5\Contracts\Ui5RegistryInterface; |
| 17: | use LaravelUi5\Core\Ui5CoreServiceProvider; |
| 18: | use ReflectionClass; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | abstract class AbstractManifest implements LaravelUi5ManifestInterface |
| 29: | { |
| 30: | |
| 31: | public function __construct(protected Ui5RegistryInterface $registry) |
| 32: | { |
| 33: | } |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | public function getFragment(string $module): array |
| 46: | { |
| 47: | if (!$this->registry->hasModule($module)) { |
| 48: | throw new InvalidModuleException($module); |
| 49: | } |
| 50: | |
| 51: | $resolved = $this->registry->getModule($module); |
| 52: | $namespace = $resolved->getArtifactRoot()->getNamespace(); |
| 53: | |
| 54: | $core = [ |
| 55: | LaravelUi5ManifestKeys::META => $this->buildMeta(), |
| 56: | LaravelUi5ManifestKeys::ROUTES => $this->buildRoutes(), |
| 57: | LaravelUi5ManifestKeys::ACTIONS => $this->buildActions($resolved), |
| 58: | LaravelUi5ManifestKeys::RESOURCES => $this->buildResources($resolved), |
| 59: | LaravelUi5ManifestKeys::SETTINGS => $this->buildSettings($namespace), |
| 60: | LaravelUi5ManifestKeys::VENDOR => $this->enhanceFragment($module), |
| 61: | LaravelUi5ManifestKeys::SHELL => $this->buildShell(), |
| 62: | ]; |
| 63: | |
| 64: | return array_filter($core, fn($value) => !empty($value)); |
| 65: | } |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | abstract protected function enhanceFragment(string $module): array; |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | private function buildMeta(): array |
| 82: | { |
| 83: | return ['generator' => 'LaravelUi5 Core'] + config('ui5.meta', []); |
| 84: | } |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | private function buildRoutes(): array |
| 92: | { |
| 93: | return array_map(fn($name) => route($name), config('ui5.routes', [])); |
| 94: | } |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | private function buildActions(Ui5ModuleInterface $module): array |
| 104: | { |
| 105: | $actions = []; |
| 106: | foreach ($module->getActions() as $action) { |
| 107: | if (!$action->getMethod()->isValidUi5ActionMethod()) { |
| 108: | throw new InvalidHttpMethodActionException($action->getNamespace(), $action->getMethod()->label()); |
| 109: | } |
| 110: | |
| 111: | $uri = collect($this->getPathParameters($action->getHandler())) |
| 112: | ->map(fn(string $parameter) => "/{{$parameter}}") |
| 113: | ->implode(''); |
| 114: | |
| 115: | $slug = ArtifactType::urlKeyFromArtifact($action); |
| 116: | |
| 117: | $prefix = Ui5CoreServiceProvider::UI5_ROUTE_PREFIX; |
| 118: | |
| 119: | $actions[$action->getSlug()] = [ |
| 120: | 'method' => $action->getMethod()->label(), |
| 121: | 'url' => "/{$prefix}/{$slug}{$uri}" |
| 122: | ]; |
| 123: | }; |
| 124: | |
| 125: | return $actions; |
| 126: | } |
| 127: | |
| 128: | private function buildResources(Ui5ModuleInterface $module): array |
| 129: | { |
| 130: | $resources = []; |
| 131: | foreach ($module->getResources() as $resource) { |
| 132: | $provider = $resource->getProvider(); |
| 133: | |
| 134: | if ($provider instanceof ParameterizableInterface) { |
| 135: | $uri = collect($this->getPathParameters($provider)) |
| 136: | ->map(fn(string $parameter) => "/{{$parameter}}") |
| 137: | ->implode(''); |
| 138: | |
| 139: | $slug = ArtifactType::urlKeyFromArtifact($resource); |
| 140: | |
| 141: | $prefix = Ui5CoreServiceProvider::UI5_ROUTE_PREFIX; |
| 142: | |
| 143: | $resources[$resource->getSlug()] = [ |
| 144: | 'method' => 'GET', |
| 145: | 'url' => "/{$prefix}/{$slug}{$uri}" |
| 146: | ]; |
| 147: | } |
| 148: | } |
| 149: | |
| 150: | return $resources; |
| 151: | } |
| 152: | |
| 153: | private function buildSettings(string $namespace): array |
| 154: | { |
| 155: | return collect($this->registry->settings($namespace)) |
| 156: | ->map(fn($setting) => $setting['default']) |
| 157: | ->all(); |
| 158: | } |
| 159: | |
| 160: | private function buildShell(): array |
| 161: | { |
| 162: | |
| 163: | if ($this instanceof Ui5ShellFragmentInterface) { |
| 164: | return $this->buildShellFragment(); |
| 165: | } |
| 166: | |
| 167: | return []; |
| 168: | } |
| 169: | |
| 170: | |
| 171: | |
| 172: | private function getPathParameters(ParameterizableInterface $target): array |
| 173: | { |
| 174: | $reflection = new ReflectionClass($target); |
| 175: | $attributes = $reflection->getAttributes(Parameter::class); |
| 176: | $parameters = []; |
| 177: | foreach ($attributes as $attr) { |
| 178: | |
| 179: | $attribute = $attr->newInstance(); |
| 180: | if (ParameterSource::Path === $attribute->source) { |
| 181: | $parameters[] = $attribute->uriKey; |
| 182: | } else { |
| 183: | throw new InvalidParameterSourceException($attribute->name, $attribute->source->label()); |
| 184: | } |
| 185: | } |
| 186: | return $parameters; |
| 187: | } |
| 188: | } |
| 189: | |