2017 © Pedro Peláez
 

library producer

Simple rule-based class resolution

image

deefour/producer

Simple rule-based class resolution

  • Wednesday, March 16, 2016
  • by deefour
  • Repository
  • 1 Watchers
  • 2 Stars
  • 907 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 5 % Grown

The README.md

Producer

Build Status Packagist Version Code Climate License, (*1)

A small class factory., (*2)

Getting Started

Run the following to add Producer to your project's composer.json. See Packagist for specific versions., (*3)

composer require deefour/producer

>=PHP5.5.0 is required., (*4)

Overview

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., (*5)

Producers and Producibles

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., (*6)

Given the following classes, (*7)

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"., (*8)

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

Resolving Producibles

The default producible resolver on the produciton factory looks like this, (*9)

get_class($producer) . ucfirst($type)

This can be customized by implementing a resolve() method on the producer passed into the factory., (*10)

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., (*11)

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., (*12)

use App\Podcast;
use Deefour\Producer\Factory;

$podcast = new Podcast();
$factory = new Factory();

$factory->resolve($podcast, 'policy'); //=> 'App\Policies\PodcastPolicy`

Making Producibles

The default producible instantiator on the production factory looks like this, (*13)

new $producible($producer);

This can be customized by implementing a make() method on the producer passed into the factory., (*14)

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., (*15)

Contribute

  • Issue Tracker: https://github.com/deefour/producer/issues
  • Source Code: https://github.com/deefour/producer

Changelog

1.0.0 - October 7, 2015

  • Release 1.0.0.

0.1.1 - August 8, 2015

  • Added ProductionFactory interface to allow more lenient type-hinting within other packages.
  • Docblock cleanup.

0.1.0 - August 4, 2015

  • Initial release.

License

Copyright (c) 2015 Jason Daly (deefour). Released under the MIT License., (*16)

The Versions

16/03 2016

dev-master

9999999-dev https://github.com/deefour/producer

Simple rule-based class resolution

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel producer deefour class resolution

16/03 2016

1.1.1

1.1.1.0 https://github.com/deefour/producer

Simple rule-based class resolution

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel producer deefour class resolution

19/02 2016

1.1.0

1.1.0.0 https://github.com/deefour/producer

Simple rule-based class resolution

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel producer deefour class resolution

20/10 2015

1.0.1

1.0.1.0 https://github.com/deefour/producer

Simple rule-based class resolution

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel producer deefour class resolution

07/10 2015

1.0.0

1.0.0.0 https://github.com/deefour/producer

Simple rule-based class resolution

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel producer deefour class resolution

08/08 2015

dev-develop

dev-develop https://github.com/deefour/producer

Simple rule-based class resolution

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel producer deefour class resolution

08/08 2015

0.2.0

0.2.0.0 https://github.com/deefour/producer

Simple rule-based class resolution

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel producer deefour class resolution

05/08 2015

0.1.0

0.1.0.0 https://github.com/deefour/producer

Simple rule-based class resolution

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel producer deefour class resolution