Package Data | |
---|---|
Maintainer Username: | deefour |
Maintainer Contact: | jason@deefour.me (Jason Daly) |
Package Create Date: | 2015-08-05 |
Package Last Update: | 2016-03-16 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-18 03:02:37 |
Package Statistics | |
---|---|
Total Downloads: | 1,928 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
A small class factory.
Run the following to add Producer to your project's composer.json
. See Packagist for specific versions.
composer require deefour/producer
>=PHP5.5.0
is required.
A Producer
is a class that resolves the FQCN of related Producible
classes. The Factory
accepts a Producer
and "type", and can instantiate a concrete Producible
class based on the resolved FQCN returned.
The production factory only accepts classes that implement Deefour\Producer\Contracts\Producer
. An exception will be thrown if the resolved class does not implement Deefour\Producer\Contracts\Producible
.
Given the following classes
use Deefour\Producer\Contracts\Producer;
use Deefour\Producer\Contracts\Producible;
class Podcast implements Producer
{
// ...
}
class PodcastPolicy implements Producible
{
// ...
}
class PodcastScope implements Producible
{
// ...
}
the production factory can produce an instance of each producible above when given a podcast and "type".
use Deefour\Producer\Factory;
$podcast = new Podcast();
$factory = new Factory();
$factory->resolve($podcast, 'policy'); //=> 'PodcastPolicy`
$factory->resolve($podcast, 'scope'); //=> 'PodcastScope`
$factory->make($podcast, 'policy'); //=> instance of PodcastPolicy
The default producible resolver on the produciton factory looks like this
get_class($producer) . ucfirst($type)
This can be customized by implementing a resolve()
method on the producer passed into the factory.
use Deefour\Producer\Contracts\Producer;
class Podcast implements Producer
{
public function resolve($type)
{
// return FQCN string here
}
}
This deefour/producer
package also comes with a more opinionated resolver at Deefour\Producer\ResolvesProducibles
.
namespace App;
use Deefour\Producer\ResolvesProducibles;
use Deefour\Producer\Contracts\Producer;
class Podcast implements Producer
{
use ResolvesProducibles;
}
this will pluralize the "type" passed in and append that to the namespace of the producer doing the class resolution.
use App\Podcast;
use Deefour\Producer\Factory;
$podcast = new Podcast();
$factory = new Factory();
$factory->resolve($podcast, 'policy'); //=> 'App\Policies\PodcastPolicy`
The default producible instantiator on the production factory looks like this
new $producible($producer);
This can be customized by implementing a make()
method on the producer passed into the factory.
use Deefour\Producer\Contracts\Producer;
class Podcast implements Producer
{
public function make($producible)
{
// instantiate the passed $producible (an FQCN)
}
}
Note: The Deefour\Producer\ResolvesProducibles
trait does not implement the make()
method.
ProductionFactory
interface to allow more lenient type-hinting within other packages.Copyright (c) 2015 Jason Daly (deefour). Released under the MIT License.