composerFilePath = $pathOrContent; } else { $content = $pathOrContent; } if ($content === false) { throw new PluginComposerFileNotFoundException( "Couldn't found composer.json file at " . $pathOrContent ); } $json = $this->validate($content); $this->populate($json); $this->composerJson = $json; } public function getStatus(): Status { return $this->status; } public function setStatus(Status $status): Context { if ($status != $this->status && $this->composerFilePath !== null) { $this->composerJson->extra->status = strtolower($status->value); file_put_contents( $this->composerFilePath, json_encode( $this->composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) ); } $this->status = $status; return $this; } /** @return Iterator */ public function getSupportChannels(): Iterator { return new ArrayIterator($this->supportChannels); } /** @return Iterator */ public function getLicenses(): Iterator { return new ArrayIterator($this->licenses); } /** @return Iterator */ public function getAuthors(): Iterator { return new ArrayIterator($this->authors); } private function validate(string $jsonFileContent): object { try { $json = json_decode( $jsonFileContent, null, 512, JSON_THROW_ON_ERROR ); } catch (JsonException $e) { throw new InvalidPluginComposerJsonFileException( message: "Failed to decode composer.json file with following error: " . $e->getMessage(), previous: $e ); } if (!isset($json->type) || !in_array($json->type, ['aikeedo-plugin', 'aikeedo-theme'])) { throw new InvalidPluginComposerJsonFileException( "Invalid composer.json file: " . " is not set or is neither of aikeedo-plugin or aikeedo-theme." ); } if (!isset($json->name)) { throw new InvalidPluginComposerJsonFileException( "Invalid composer.json file: is not set." ); } if ( $json->type == 'aikeedo-plugin' && (!isset($json->extra->{'entry-class'}) || !$json->extra->{'entry-class'}) ) { throw new InvalidPluginComposerJsonFileException( "Invalid composer.json file: is not set." ); } if (!isset($json->require->{"heyaikeedo/composer"})) { throw new InvalidPluginComposerJsonFileException( "Invalid composer.json file: " . " All plugins/themes must require library." ); } return $json; } private function populate(object $json): void { // @phpstan-ignore-next-line $this->type = $json->type === 'aikeedo-theme' ? Type::THEME : Type::PLUGIN; // @phpstan-ignore-next-line $this->name = new Name($json->name); $this->status = isset($json->extra->status) ? Status::from($json->extra->status) : Status::INACTIVE; // @phpstan-ignore-next-line $this->description = new Description( $json->description ?? null ); // @phpstan-ignore-next-line $this->version = new Version($json->version ?? null); // @phpstan-ignore-next-line $this->homepage = new Url($json->homepage ?? null); // @phpstan-ignore-next-line $this->releasedAt = isset($json->time) && $json->time ? new DateTime($json->time) : null; // @phpstan-ignore-next-line $this->tagline = new Tagline($json->extra->tagline ?? null); // @phpstan-ignore-next-line $this->title = new Title($json->extra->title ?? null); // @phpstan-ignore-next-line $this->icon = new Url($json->extra->icon ?? null); // @phpstan-ignore-next-line $this->logo = new Url($json->extra->logo ?? null); // @phpstan-ignore-next-line $this->entryClass = new EntryClass($json->extra->{'entry-class'} ?? null); // Populate support channels $supportChannels = []; if (isset($json->support)) { if (isset($json->support->chat)) { $supportChannels[] = new SupportChannel( SupportChannelType::CHAT, $json->support->chat ); } if (isset($json->support->docs)) { $supportChannels[] = new SupportChannel( SupportChannelType::DOCS, $json->support->docs ); } if (isset($json->support->email)) { $supportChannels[] = new SupportChannel( SupportChannelType::EMAIL, $json->support->email ); } if (isset($json->support->forum)) { $supportChannels[] = new SupportChannel( SupportChannelType::FORUM, $json->support->forum ); } if (isset($json->support->irc)) { $supportChannels[] = new SupportChannel( SupportChannelType::IRC, $json->support->irc ); } if (isset($json->support->issues)) { $supportChannels[] = new SupportChannel( SupportChannelType::ISSUES, $json->support->issues ); } if (isset($json->support->rss)) { $supportChannels[] = new SupportChannel( SupportChannelType::RSS, $json->support->rss ); } if (isset($json->support->source)) { $supportChannels[] = new SupportChannel( SupportChannelType::SOURCE, $json->support->source ); } if (isset($json->support->wiki)) { $supportChannels[] = new SupportChannel( SupportChannelType::WIKI, $json->support->wiki ); } } // @phpstan-ignore-next-line $this->supportChannels = $supportChannels; // Populate licenses $licenses = []; if (isset($json->license)) { /** @var string|string[] $licenses */ $licenses = $json->license; if (!is_array($licenses)) { $licenses = [$licenses]; } $licenses = array_map( fn ($license) => new License($license), $licenses ); } // @phpstan-ignore-next-line $this->licenses = $licenses; // Populate authors $authors = []; if (isset($json->authors)) { $authors = array_map( fn ($author) => new Author( $author->name, $author->email ?? null, $author->homepage ?? null, $author->role ?? null ), $json->authors ); } // @phpstan-ignore-next-line $this->authors = $authors; } }