Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/decodelabs/archetype
Simple named library loader for PHP
https://github.com/decodelabs/archetype
class-loader php resolver
Last synced: 1 day ago
JSON representation
Simple named library loader for PHP
- Host: GitHub
- URL: https://github.com/decodelabs/archetype
- Owner: decodelabs
- License: mit
- Created: 2021-11-17T15:25:57.000Z (about 3 years ago)
- Default Branch: develop
- Last Pushed: 2024-09-04T20:51:15.000Z (4 months ago)
- Last Synced: 2025-01-02T13:36:24.155Z (7 days ago)
- Topics: class-loader, php, resolver
- Language: PHP
- Homepage:
- Size: 129 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Archetype
[![PHP from Packagist](https://img.shields.io/packagist/php-v/decodelabs/archetype?style=flat)](https://packagist.org/packages/decodelabs/archetype)
[![Latest Version](https://img.shields.io/packagist/v/decodelabs/archetype.svg?style=flat)](https://packagist.org/packages/decodelabs/archetype)
[![Total Downloads](https://img.shields.io/packagist/dt/decodelabs/archetype.svg?style=flat)](https://packagist.org/packages/decodelabs/archetype)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/decodelabs/archetype/integrate.yml?branch=develop)](https://github.com/decodelabs/archetype/actions/workflows/integrate.yml)
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-44CC11.svg?longCache=true&style=flat)](https://github.com/phpstan/phpstan)
[![License](https://img.shields.io/packagist/l/decodelabs/archetype?style=flat)](https://packagist.org/packages/decodelabs/archetype)### Simple named library loader for PHP
Archetype provides a generic frontend to resolving implementation classes for a named interface with an extensible plugin architechture.
_Get news and updates on the [DecodeLabs blog](https://blog.decodelabs.com/tag/decodelabs-archetype)._
---
## Installation
Install via Composer:
```bash
composer require decodelabs/archetype
```## Usage
### Importing
Archetype uses [Veneer](https://github.com/decodelabs/veneer) to provide a unified frontage under
DecodeLabs\Archetype
.
You can access all the primary functionality via this static frontage without compromising testing and dependency injection.### Functionality
Use Archetype when designing plugin oriented libraries that need to load arbitrary extension classes based on a naming convention.
By mapping names to classes in an extensible and customisable resolver structure, Archetype allows fast and reliable means to create loosely coupled plugin ecosystems.
Example:
```php
// Main library
namespace My\Library
{use DecodeLabs\Archetype;
interface Thing {}
class Factory {
public static function loadThing(string $name): Thing
{
// Resolve name to class for Thing interface
$class = Archetype::resolve(Thing::class, $name);return new $class();
}
}
}// Thing implementations
namespace My\Library\Thing
{use My\Library\Thing;
class Box implements Thing {}
class Potato implements Thing {}
class Dog implements Thing {}
}// Calling code
namespace My\App
{use My\Library\Factory;
$box = Factory::loadthing('Box');
$potato = Factory::loadThing('Potato');
$dog = Factory::loadThing('Dog');
}
```## Resolvers
Archetype uses a hierarchy of
Resolvers
to turn a name into a class. By default, theHandler
will fall back on aResolver\Generic
instance that simply locates the named class within the namespace of the associated interface.In the above example, the implementations of the
My\Library\Thing
can be found atMy\Library\Thing\\*
.### Custom resolvers
The
Resolver\Archetype
implementation however will also automatically look for customResolver
classes in the same location as the target interface, named\Archetype
.The following example will replace the default functionality and cause Archetype to look in a different location for the
Thing
implementations:```php
namespace My\Library {use DecodeLabs\Archetype\Resolver;
class ThingArchetype implements Resolver
{public function getInterface(): string
{
return Thing::class;
}public function getPriority(): int
{
return 10;
}public function resolve(string $name): ?string
{
return 'Some\\Other\\Namespace\\'.$name;
}
}
}
```### Multiple resolvers
Multiple
Resolver
instances can be stacked against a single interface, called in series based on the priority they request, the first one to return a non-null class name wins.Alternative
Resolvers
can be loaded with:```php
use DecodeLabs\Archetype;
use My\Library\Resolver\Alternative as AlternativeResolver;Archetype::register(new AlternativeResolver());
```### File lookup
Resolvers
that also implement theFinder
interface can define the means to lookup a file path based on the provided name, against the space defined by the target interface.This can be useful when the _space_ that is defined by the root interface may contain assets aside from PHP code.
It is down to the implementation to decide how to map names to file paths (there are no pre-built default
Finder
classes).Example:
```php
namespace My\Library {use DecodeLabs\Archetype\Finder;
class ThingArchetype implements Finder
{public function getInterface(): string
{
return Thing::class;
}public function getPriority(): int
{
return 10;
}public function resolve(string $name): ?string
{
return 'Some\\Other\\Namespace\\'.$name;
}public function findFile(string $name): ?string
{
return './some/other/namespace/'.$name.'.jpg';
}
}
}namespace My\App {
use DecodeLabs\Archetype;
use My\Library\Thing;$boxImagePath = Archetype::findFile(Thing::class, 'box');
}
```## Licensing
Archetype is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.