Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions formwork/src/Cms/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Formwork\Cms;

use Formwork\Config\Config;
use Formwork\Data\Attributes\Getter;
use Formwork\Data\Attributes\Setter;
use Formwork\Files\FileCollection;
use Formwork\Files\FileFactory;
use Formwork\Languages\Languages;
use Formwork\Languages\LanguagesFactory;
use Formwork\Metadata\MetadataCollection;
use Formwork\Model\Attributes\ReadonlyModelProperty;
use Formwork\Model\Model;
use Formwork\Pages\ContentFile;
use Formwork\Pages\Exceptions\PageNotFoundException;
Expand Down Expand Up @@ -47,25 +48,21 @@ class Site extends Model implements Stringable
/**
* Site content file
*/
#[ReadonlyModelProperty]
protected ?ContentFile $contentFile = null;

/**
* Site route
*/
#[ReadonlyModelProperty]
protected ?string $route = '/';

/**
* Site canonical route
*/
#[ReadonlyModelProperty]
protected ?string $canonicalRoute = null;

/**
* Site slug
*/
#[ReadonlyModelProperty]
protected ?string $slug = '';

/**
Expand All @@ -76,19 +73,16 @@ class Site extends Model implements Stringable
/**
* Site templates
*/
#[ReadonlyModelProperty]
protected Templates $templates;

/**
* Site users
*/
#[ReadonlyModelProperty]
protected Users $users;

/**
* Site last modified time
*/
#[ReadonlyModelProperty]
protected int $lastModifiedTime;

/**
Expand All @@ -101,7 +95,7 @@ class Site extends Model implements Stringable
*
* @var array<string, Page>
*/
#[ReadonlyModelProperty]
#[Getter]
protected array $storage = [];

/**
Expand All @@ -119,7 +113,6 @@ class Site extends Model implements Stringable
/**
* Site files
*/
#[ReadonlyModelProperty]
protected FileCollection $files;

/**
Expand Down Expand Up @@ -163,6 +156,7 @@ public function inclusiveSiblings(): PageCollection
/**
* Get site path
*/
#[Getter]
public function path(): ?string
{
return $this->path;
Expand All @@ -171,11 +165,13 @@ public function path(): ?string
/**
* Get site filename
*/
#[Getter]
public function contentFile(): ?ContentFile
{
return $this->contentFile;
}

#[Getter]
public function contentPath(): ?string
{
return $this->contentPath;
Expand All @@ -189,6 +185,7 @@ public function contentRelativePath(): ?string
/**
* Get site route
*/
#[Getter]
public function route(): ?string
{
return $this->route;
Expand All @@ -197,6 +194,7 @@ public function route(): ?string
/**
* Get site canonical route
*/
#[Getter]
public function canonicalRoute(): ?string
{
return $this->canonicalRoute;
Expand All @@ -205,6 +203,7 @@ public function canonicalRoute(): ?string
/**
* Get site slug
*/
#[Getter]
public function slug(): ?string
{
return $this->slug;
Expand All @@ -213,6 +212,7 @@ public function slug(): ?string
/**
* Get site languages
*/
#[Getter]
public function languages(): Languages
{
return $this->languages;
Expand All @@ -221,6 +221,7 @@ public function languages(): Languages
/**
* Get site templates
*/
#[Getter]
public function templates(): Templates
{
return $this->templates;
Expand All @@ -229,6 +230,7 @@ public function templates(): Templates
/**
* Get site users
*/
#[Getter]
public function users(): Users
{
return $this->users;
Expand All @@ -245,6 +247,7 @@ public function schemes(): Schemes
/**
* Get the current page of the site
*/
#[Getter]
public function currentPage(): ?Page
{
return $this->currentPage;
Expand All @@ -255,6 +258,7 @@ public function currentPage(): ?Page
*
* @return array<string, string>
*/
#[Getter]
public function routeAliases(): array
{
return $this->routeAliases;
Expand All @@ -263,6 +267,7 @@ public function routeAliases(): array
/**
* Get site metadata
*/
#[Getter]
public function metadata(): MetadataCollection
{
if (isset($this->metadata)) {
Expand All @@ -282,6 +287,7 @@ public function metadata(): MetadataCollection
/**
* Get site last modified time
*/
#[Getter]
public function lastModifiedTime(): ?int
{
if ($this->contentPath === null) {
Expand Down Expand Up @@ -462,6 +468,7 @@ public function resolveRouteAlias(string $route): ?string
/**
* Set and return site current page
*/
#[Setter]
public function setCurrentPage(Page $page): Page
{
return $this->currentPage = $page;
Expand All @@ -470,6 +477,7 @@ public function setCurrentPage(Page $page): Page
/**
* Get site files
*/
#[Getter]
public function files(): FileCollection
{
if (isset($this->files)) {
Expand Down Expand Up @@ -517,6 +525,7 @@ public function load(): void
*
* @param array{available: list<string>, httpPreferred: bool, default?: string} $config
*/
#[Setter]
protected function setLanguages(array $config): void
{
$this->data['languages'] = $config;
Expand All @@ -530,6 +539,7 @@ protected function setLanguages(array $config): void
/**
* @param array<string, mixed> $metadata
*/
#[Setter]
protected function setMetadata(array $metadata): void
{
$this->data['metadata'] = $metadata;
Expand All @@ -538,6 +548,7 @@ protected function setMetadata(array $metadata): void
/**
* Set site path
*/
#[Setter]
protected function setPath(string $path): void
{
$this->path = $this->data['path'] = $path;
Expand All @@ -546,6 +557,7 @@ protected function setPath(string $path): void
/**
* Set site content path
*/
#[Setter]
protected function setContentPath(string $path): void
{
$this->contentPath = FileSystem::normalizePath($path . '/');
Expand All @@ -556,6 +568,7 @@ protected function setContentPath(string $path): void
*
* @param array<string, string> $aliases
*/
#[Setter]
protected function setRouteAliases(array $aliases): void
{
foreach ($aliases as $from => $to) {
Expand Down
26 changes: 11 additions & 15 deletions formwork/src/Data/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Countable;
use Formwork\Data\Contracts\Arrayable;
use Formwork\Data\Traits\DataArrayable;
use Formwork\Data\Traits\DataCountableIterator;
use Formwork\Data\Traits\DataMultipleGetter;
use Formwork\Data\Traits\DataMultipleSetter;
Expand All @@ -20,22 +19,14 @@
*/
abstract class AbstractCollection implements Arrayable, Countable, Iterator
{
use DataArrayable;

/** @use DataCountableIterator<array<int|string, T>> */
use DataCountableIterator;

/** @use DataMultipleGetter<array<string, T>> */
use DataMultipleGetter {
has as protected baseHas;
get as protected baseGet;
}
use DataMultipleGetter;

/** @use DataMultipleSetter<array<string, T>> */
use DataMultipleSetter {
set as protected baseSet;
remove as protected baseRemove;
}
use DataMultipleSetter;

/**
* @var array<int|string, T>
Expand Down Expand Up @@ -718,7 +709,7 @@ public function has(string $key): bool
if (!$this->isAssociative()) {
throw new LogicException('Value presence can be checked only in associative collections');
}
return $this->baseHas($key);
return Arr::has($this->data, $key);
}

/** Get a collection item by the given key
Expand All @@ -740,7 +731,7 @@ public function get(string $key, mixed $default = null): mixed
if (!$this->isAssociative()) {
throw new LogicException('Values can be get only from associative collections');
}
return $this->baseGet($key, $default);
return Arr::get($this->data, $key, $default);
}

/**
Expand All @@ -766,7 +757,7 @@ public function set(string $key, mixed $value): void
$this->data[$key] = null;
}

$this->baseSet($key, $value);
Arr::set($this->data, $key, $value);
}

/**
Expand All @@ -779,7 +770,7 @@ public function remove(string $key): void
if (!$this->isAssociative() || !$this->isMutable()) {
throw new LogicException('Values can be removed only from associative and mutable collections');
}
$this->baseRemove($key);
Arr::remove($this->data, $key);
}

/**
Expand All @@ -805,4 +796,9 @@ public function merge(self $collection): void

$this->data = [...$this->data, ...$collection->data];
}

public function toArray(): array
{
return $this->data;
}
}
17 changes: 17 additions & 0 deletions formwork/src/Data/Attributes/Getter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Formwork\Data\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)]
class Getter
{
/**
* @param non-empty-string|null $key
*/
public function __construct(
public readonly ?string $key = null,
public readonly bool $export = true
) {}
}
16 changes: 16 additions & 0 deletions formwork/src/Data/Attributes/Setter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Formwork\Data\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)]
class Setter
{
/**
* @param non-empty-string|null $key
*/
public function __construct(
public readonly ?string $key = null,
) {}
}
Loading