Files
michaelschiemer/src/Framework/Discovery/ValueObjects/TemplateMapping.php
Michael Schiemer 55a330b223 Enable Discovery debug logging for production troubleshooting
- Add DISCOVERY_LOG_LEVEL=debug
- Add DISCOVERY_SHOW_PROGRESS=true
- Temporary changes for debugging InitializerProcessor fixes on production
2025-08-11 20:13:26 +02:00

138 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Discovery\ValueObjects;
use App\Framework\Core\ValueObjects\Byte;
use App\Framework\Filesystem\FilePath;
/**
* Immutable value object for template mappings
* Replaces simple key-value arrays with memory-efficient typed structure
*/
final readonly class TemplateMapping
{
public function __construct(
public string $name,
public FilePath $path,
public string $type = 'view'
) {
}
public static function create(
string $name,
string $path,
string $type = 'view'
): self {
return new self(
name: $name,
path: FilePath::create($path),
type: $type
);
}
public static function fromPath(FilePath $path): self
{
$name = basename($path->toString(), '.php');
$type = str_contains($path->toString(), '/views/') ? 'view' : 'template';
return new self(
name: $name,
path: $path,
type: $type
);
}
/**
* Get unique identifier for deduplication
*/
public function getUniqueId(): string
{
return $this->name . '::' . $this->type;
}
/**
* Check if this mapping is the same as another
*/
public function isSameAs(self $other): bool
{
return $this->name === $other->name && $this->type === $other->type;
}
/**
* Check if template matches name pattern
*/
public function matchesName(string $pattern): bool
{
return fnmatch($pattern, $this->name);
}
/**
* Get template extension
*/
public function getExtension(): string
{
return pathinfo($this->path->toString(), PATHINFO_EXTENSION);
}
/**
* Check if template is of specific type
*/
public function isType(string $type): bool
{
return $this->type === $type;
}
/**
* Convert to array for serialization
*/
public function toArray(): array
{
return [
'name' => $this->name,
'path' => $this->path->toString(),
'type' => $this->type,
];
}
/**
* Create from array data
*/
public static function fromArray(array $data): self
{
// Handle both string paths and FilePath objects/arrays from old cache data
$path = $data['path'];
if (is_array($path)) {
// Old cache format: FilePath object was serialized as array
$path = $path['path'] ?? (string) reset($path);
} elseif (! is_string($path)) {
// Fallback: convert to string
$path = (string) $path;
}
// Skip invalid/empty paths from corrupted cache data
if (empty(trim($path))) {
throw new \InvalidArgumentException("Cannot create TemplateMapping with empty path");
}
return new self(
name: $data['name'],
path: FilePath::create($path),
type: $data['type'] ?? 'view'
);
}
/**
* Get memory footprint estimate
*/
public function getMemoryFootprint(): Byte
{
$bytes = strlen($this->name) +
strlen($this->path->toString()) +
strlen($this->type);
return Byte::fromBytes($bytes);
}
}