Skip to content

Commit

Permalink
Redesigned Setup API & overwrite constraint handling
Browse files Browse the repository at this point in the history
- Merged Setup with Builder
- Moved decorator feature to separate Setup method
- Added composedInstance Entry method
- Added Wrapper builder for decorator and compose methods
- Replaced overwrite flag with explicit "replace" methods
- Redesigned abstraction dependencies and inheritance
- Added static constructors for simple and validated setup
- Moved setup exceptions to Setup namespace
- Minor refactorings
  • Loading branch information
shudd3r committed Nov 25, 2019
2 parents 2d6ca66 + c93bfc1 commit c4bce6b
Show file tree
Hide file tree
Showing 18 changed files with 928 additions and 586 deletions.
347 changes: 190 additions & 157 deletions README.md

Large diffs are not rendered by default.

74 changes: 0 additions & 74 deletions src/Builder.php

This file was deleted.

136 changes: 0 additions & 136 deletions src/Builder/Entry.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/Exception/RecordNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,4 @@ public static function undefined(string $id): self
{
return new self("Record `$id` not defined");
}

public static function cannotWrap(string $id): self
{
throw new self("Attempted to decorate non-existent `$id` record with new composition");
}
}
51 changes: 51 additions & 0 deletions src/Records/Record/ComposedInstanceRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of Polymorphine/Container package.
*
* (c) Shudd3r <q3.shudder@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Polymorphine\Container\Records\Record;

use Polymorphine\Container\Records\Record;
use Psr\Container\ContainerInterface;


class ComposedInstanceRecord implements Record
{
private $recursiveId;
private $baseRecord;
private $wrappers;
private $cached;

public function __construct(string $id, Record $baseRecord, array $wrappers)
{
$this->recursiveId = $id;
$this->baseRecord = $baseRecord;
$this->wrappers = $wrappers;
}

public function value(ContainerInterface $container)
{
if ($this->cached) { return $this->cached; }
$current = $this->baseRecord->value($container);
foreach ($this->wrappers as [$className, $dependencies]) {
$current = new $className(...$this->mapContainerRecords($current, $dependencies, $container));
}

return $this->cached = $current;
}

private function mapContainerRecords($current, array $identifiers, ContainerInterface $container): array
{
$dependencies = [];
foreach ($identifiers as $id) {
$dependencies[] = $id === $this->recursiveId ? $current : $container->get($id);
}
return $dependencies;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@
use Psr\Container\ContainerInterface;


trait ExtractArgumentsTrait
trait ContainerMapMethod
{
private function arguments(array $identifiers, ContainerInterface $container): array
private function containerValues(array $identifiers, ContainerInterface $container): array
{
$arguments = [];
foreach ($identifiers as $id) {
$arguments[] = $container->get($id);
}

return $arguments;
return array_map(function ($id) use ($container) { return $container->get($id); }, $identifiers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
*
* Returned value is cached and returned directly on subsequent calls.
*/
class ComposeRecord implements Record
class InstanceRecord implements Record
{
use ExtractArgumentsTrait;
use ContainerMapMethod;

private $className;
private $dependencies;
Expand All @@ -46,6 +46,6 @@ public function value(ContainerInterface $container)

private function create(ContainerInterface $container)
{
return new $this->className(...$this->arguments($this->dependencies, $container));
return new $this->className(...$this->containerValues($this->dependencies, $container));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
*
* Returned value is cached and returned directly on subsequent calls.
*/
class CreateMethodRecord implements Record
class ProductRecord implements Record
{
use ExtractArgumentsTrait;
use ContainerMapMethod;

private $factoryId;
private $method;
Expand All @@ -51,6 +51,6 @@ public function value(ContainerInterface $container)
private function create(ContainerInterface $container)
{
$factory = $container->get($this->factoryId);
return $factory->{$this->method}(...$this->arguments($this->arguments, $container));
return $factory->{$this->method}(...$this->containerValues($this->arguments, $container));
}
}
Loading

0 comments on commit c4bce6b

Please sign in to comment.