Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5da7fcc
Replaced exception interfaces with concretes
mecha Nov 14, 2019
ceb0e24
Module throws specific exceptions
mecha Nov 14, 2019
313449f
Added factories
mecha Nov 14, 2019
ea61d8c
Updated PHPStorm config for PHPUnit
mecha Nov 14, 2019
b2dd05d
Added description of `FactoryInterface` to readme
mecha Nov 14, 2019
4192ea3
Added return type hint for `getDependencies()`
mecha Nov 14, 2019
e60ba21
Added missing test for `Value`
mecha Nov 14, 2019
e8d6a80
Added `Extension` factory
mecha Nov 15, 2019
d69654e
Added `Alias` factory
mecha Nov 15, 2019
9028ad2
Added `InterpolatedString` factory
mecha Nov 15, 2019
e638240
Added `GlobalVar` factory
mecha Nov 15, 2019
17dc745
Renamed and optimized `InterpolatedString`
mecha Nov 15, 2019
772afd6
Corrected use of double quotes for param default
mecha Nov 15, 2019
26af8d0
Updated dependencies and fixed project config
mecha Feb 10, 2020
ac1fbde
Fixed PHPUnit config
mecha Feb 10, 2020
f7cf001
Reworked interfaces and exceptions
mecha Feb 10, 2020
389ffb8
Updated implementations and tests after rework
mecha Feb 10, 2020
06f8a2d
Added `ArrayExtension` implementation
mecha Feb 10, 2020
209b759
Removed void return for 7.0 compatibility
mecha Feb 11, 2020
9f4f3e6
Added `ServiceInterface::withDependencies`
mecha Feb 11, 2020
39f8623
Invocation args are passed to callbacks
mecha Feb 12, 2020
22fc398
Renamed `Invocable` to `Func`
mecha Feb 12, 2020
5159614
Removed exceptions
mecha Feb 12, 2020
875bee2
Removed unused test helper trait
mecha Feb 12, 2020
0aa2ccb
Removed useless interface tests
mecha Feb 12, 2020
589da5b
Added `ModularInterface`
mecha Feb 12, 2020
d7c717a
Removed useless assertions
mecha Feb 12, 2020
635cb04
Updated dependencies
mecha Feb 12, 2020
ad4f440
Added `PrefixingModule` decorator
mecha Feb 12, 2020
687b682
Added `ModularModule`
mecha Feb 12, 2020
cdf8a6e
Changed namespaces to `Mecha` vendor
mecha Feb 12, 2020
ea5eb13
Removed shit from the repo
mecha Feb 12, 2020
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
Prev Previous commit
Next Next commit
Updated implementations and tests after rework
  • Loading branch information
mecha committed Feb 10, 2020
commit 389ffb880ef1b680395fe84318d6b31f9d817d9f
42 changes: 0 additions & 42 deletions src/Factories/AbstractFactory.php

This file was deleted.

53 changes: 0 additions & 53 deletions src/Factories/Callback.php

This file was deleted.

57 changes: 57 additions & 0 deletions src/Services/AbstractService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Dhii\Modular\Module\Services;

use Dhii\Modular\Module\ServiceInterface;
use Psr\Container\ContainerInterface;

/**
* A partial implementation of a service, that is aware of a list of dependency keys.
*
* @since [*next-version*]
*/
abstract class AbstractService implements ServiceInterface
{
/**
* @since [*next-version*]
*
* @var string[]
*/
protected $dependencies;

/**
* Constructor.
*
* @since [*next-version*]
*
* @param string[] $dependencies The keys of dependent services.
*/
public function __construct(array $dependencies)
{
$this->dependencies = $dependencies;
}

/**
* @inheritdoc
*
* @since [*next-version*]
*/
public function getDependencies() : array
{
return $this->dependencies;
}

/**
* Resolves the dependency keys to their corresponding values from a container.
*
* @since [*next-version*]
*
* @param ContainerInterface $c The container from which to resolve service values.
*
* @return array An array of service values, in the same order as in {@link AbstractService::$dependencies}.
*/
protected function resolveDependencies(ContainerInterface $c) : array
{
return array_map([$c, 'get'], $this->dependencies);
}
}
18 changes: 17 additions & 1 deletion src/Factories/Alias.php → src/Services/Alias.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
<?php

namespace Dhii\Modular\Module\Factories;
namespace Dhii\Modular\Module\Services;

use Dhii\Modular\Module\FactoryInterface;
use Psr\Container\ContainerInterface;

/**
* A service factory that simply resolves to another existing service, and can optionally default to another factory.
*
* Example usage:
* ```
* [
* 'service_a' => new Alias('service_b'),
* 'service_b' => function() {
* return "hello";
* }
* 'service_c' => new Alias('service_d', function () {
* return "default";
* }),
* ]
*
* $c->get('service_a'); // "hello"
* $c->get('service_c'); // "default"
* ```
*
* @since [*next-version*]
*/
class Alias implements FactoryInterface
Expand Down
8 changes: 4 additions & 4 deletions src/Factories/Extension.php → src/Services/Extension.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace Dhii\Modular\Module\Factories;
namespace Dhii\Modular\Module\Services;

use Dhii\Modular\Module\ExtensionInterface;
use Psr\Container\ContainerInterface;

class Extension extends AbstractFactory
class Extension extends AbstractService implements ExtensionInterface
{
/**
* @since [*next-version*]
Expand Down Expand Up @@ -37,8 +38,7 @@ public function __construct(array $dependencies, callable $factoryFn)
*/
public function __invoke(ContainerInterface $c, $prev = null)
{
$deps = array_map([$c, 'get'], $this->dependencies);
$args = array_merge([$prev], $deps);
$args = array_merge([$prev], $this->resolveDependencies($c));

return call_user_func_array($this->factoryFn, $args);
}
Expand Down
24 changes: 21 additions & 3 deletions src/Factories/Service.php → src/Services/Factory.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
<?php

namespace Dhii\Modular\Module\Factories;
namespace Dhii\Modular\Module\Services;

use Dhii\Modular\Module\FactoryInterface;
use Psr\Container\ContainerInterface;

/**
* A standard service factory implementation.
*
* Example usage:
* ```
* [
* 'service_a' => new Factory([], function () {
* return 'hello';
* }),
* 'service_b' => new Factory([], function () {
* return 'world';
* }),
* 'service_c' => new Factory(['service_a', 'service_b'], function ($a, $b) {
* return "$a $b";
* }),
* ]
*
* $c->get('service_c'); // "hello world"
* ```
*
* @since [*next-version*]
*/
class Service extends AbstractFactory
class Factory extends AbstractService implements FactoryInterface
{
/**
* @since [*next-version*]
Expand Down Expand Up @@ -41,6 +59,6 @@ public function __construct(array $dependencies, callable $factoryFn)
*/
public function __invoke(ContainerInterface $c)
{
return call_user_func_array($this->factoryFn, array_map([$c, 'get'], $this->dependencies));
return call_user_func_array($this->factoryFn, $this->resolveDependencies($c));
}
}
29 changes: 13 additions & 16 deletions src/Factories/FormatStr.php → src/Services/FormatStr.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
<?php

namespace Dhii\Modular\Module\Factories;
namespace Dhii\Modular\Module\Services;

use Dhii\Modular\Module\FactoryInterface;
use Psr\Container\ContainerInterface;

/**
* A factory for string values. Supports interpolation with dependent service values.
*
* Example usage:
*
* ```
* // Simple example
* new FormatStr('Hello world');
*
* // Numeric dependencies example
* new FormatStr('User name is: {0}', ['user_name_service']);
*
* // Associative dependencies example
* new FormatStr('{day} {month}', [
* 'day' => 'date/day',
* 'month' => 'date/month',
* ]);
* ```
* ```
* [
* 'service_a' => new FormatStr('John Smith'),
* 'service_b' => new FormatStr('User name is: {0}', ['service_a']),
* 'service_c' => new FormatStr('{day} {month}', [
* 'day' => 'date/day',
* 'month' => 'date/month',
* ]),
* ]
* ```
*
* @since [*next-version*]
*/
class FormatStr extends AbstractFactory
class FormatStr extends AbstractService implements FactoryInterface
{
/**
* @since [*next-version*]
Expand Down
14 changes: 13 additions & 1 deletion src/Factories/GlobalVar.php → src/Services/GlobalVar.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
<?php

namespace Dhii\Modular\Module\Factories;
namespace Dhii\Modular\Module\Services;

use Dhii\Modular\Module\FactoryInterface;
use Psr\Container\ContainerInterface;

/**
* A service factory for referencing global variables.
*
* Example usage:
* ```
* global $var;
* $var = 5;
*
* [
* 'service_a' => new GlobalVar('var')
* ]
*
* $c->get('service_a'); // 5
* ```
*
* @since [*next-version*]
*/
class GlobalVar implements FactoryInterface
Expand Down
73 changes: 73 additions & 0 deletions src/Services/Invocable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Dhii\Modular\Module\Services;

use Dhii\Modular\Module\FactoryInterface;
use Psr\Container\ContainerInterface;

/**
* A factory implementation that returns an invocable function.
*
* The callback's arguments will be equivalent to the service instances that correspond with the `Invocable` instance's
* dependency keys, in the order they are given. Those service are fetched only once when the `Invocable` is invoked and
* the callback is returned, not when the _returned_ callback is invoked. They are then "cached" in the returned
* callback's scope.
*
* Example usage:
* ```
* [
* 'service_a' => function () {
* return "hello";
* },
* 'service_b' => function () {
* return "world";
* },
* 'service_c' => new Invocable(['service_a', 'service_b'], function ($a, $b) {
* return "$a $b";
* })
* ]
*
* $fn = $c->get('service_c');
* echo $fn(); // "hello world"
* ```
*
* @since [*next-version*]
*/
class Invocable extends AbstractService implements FactoryInterface
{
/**
* @since [*next-version*]
*
* @var callable
*/
protected $function;

/**
* Constructor.
*
* @since [*next-version*]
*
* @param string[] $dependencies The keys of dependent services.
* @param callable $function The function to return for this service.
*/
public function __construct(array $dependencies, callable $function)
{
parent::__construct($dependencies);

$this->function = $function;
}

/**
* @inheritdoc
*
* @since [*next-version*]
*/
public function __invoke(ContainerInterface $c)
{
$deps = $this->resolveDependencies($c);

return function () use ($deps) {
return ($this->function)(...$deps);
};
}
}
Loading