Skip to content

Commit

Permalink
Changed class structure
Browse files Browse the repository at this point in the history
- Record collections implement Records abstraction
- Moved setup classes into separate namespace
  • Loading branch information
shudd3r committed Nov 3, 2019
2 parents 6e966af + f19896d commit a7efadd
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 98 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
### Installation with [Composer](https://getcomposer.org/)
php composer.phar require polymorphine/container

### Container Setup
### Container setup
This example will show how to set up simple container. It starts with instantiating
[`ContainerSetup`](src/Setup/ContainerSetup.php) object, and using its methods to set
[`ContainerSetup`](src/Setup.php) object, and using its methods to set
container's entries:
```php
<?php
use Polymorphine\Container\Setup\ContainerSetup;
use Polymorphine\Container\Setup;

require_once __DIR__ . '/vendor/autoload.php';
require_once 'vendor/autoload.php';

$setup = new ContainerSetup();
$setup = new Setup();
````
Using `ContainerSetup::entry()` method:
```php
Expand All @@ -45,10 +45,10 @@ $setup->entry('deferred')->invoke(function (ContainerInterface $c) {
$setup->entry('composed.factory')->compose(ComposedClass::class, 'direct.object', 'deferred');
$setup->entry('factory.product')->create('composed.factory', 'create', 'domain');
```
Or passing array of [`Record`](src/Setup/Record.php) instances:
Or passing array of [`Record`](src/Records/Record.php) instances:
```php
// assumed Record namespace is also imported at the top
use Polymorphine\Container\Setup\Record;
use Polymorphine\Container\Records\Record;
// ...

$setup->records([
Expand All @@ -71,14 +71,14 @@ $container->get('factory.product'); // return type of ComposedClass::create() me
Container may be instantiated before adding any entries, and using `ContainerSetup` this
way Container instance will be mutable only through its setter methods and once stored values
will not be overwritten (except [*decorator feature*](#mutable-record---decorator-feature) described below).
It is recommended that access to [`ContainerSetup`](src/Setup/ContainerSetup.php) was encapsulated
It is recommended that access to [`ContainerSetup`](src/Setup.php) was encapsulated
within controlled scope - see: [Read and Write separation](#read-and-write-separation)

#### Records decide how it works internally
Values returned from Container are all wrapped into [`Record`](src/Setup/Record.php) abstraction
Values returned from Container are all wrapped into [`Record`](src/Records/Record.php) abstraction
that allows for different unwrapping strategies - it may be either returned directly or internally
created by calling its (lazy) initialization procedure. You may read DocBlock comments in provided
default [records](src/Setup/Record) sourcecode to get more information. Here's short explanation of
default [records](src/Records/Record) sourcecode to get more information. Here's short explanation of
package's Record implementations:

- `ValueRecord`: Just a value, that will be returned as it was passed (callbacks will be returned
Expand All @@ -89,12 +89,12 @@ package's Record implementations:
```
- `CallbackRecord`: Lazily invoked value cache. Takes callable that will be called with
`ContainerInterface` as parameter, and value of this call will be stored and returned on
subsequent calls. Setup with `invoke` method:
subsequent calls. Records with `invoke` method:
```php
$setup->entry(string $id)->invoke(callable $callback);
```
- `ComposeRecord`: Lazy instantiated object of given class. Constructor parameters are passed
as aliases to other container entries. Setup - `compose` method:
as aliases to other container entries. Records - `compose` method:
```php
$setup->entry(string $id)->compose(string $class, string ...$dependencyRecords);
```
Expand Down Expand Up @@ -128,18 +128,18 @@ can't ensure valid use and possible errors will emerge at runtime.
Using only `ContainerSetup` to set records Container will always be in valid state, because
method type hints and id validation will ensure that. Those checks can hit efficiency for
large number of records, so you might want to create instance directly or passing predefined
[`RecordCollection`](src/Setup/RecordCollection.php) into setup constructor (its good to have
[`RecordCollection`](src/Records/RecordCollection.php) into setup constructor (its good to have
some integration tests in place). This collection is the same instance that Container uses,
except setup has write access to it, and container is read only. Setup comes with two static
except setup has write access to it, and container is read only. Records comes with two static
constructors that will instantiate collection for you: `RecordSetup::prebuilt()` and
`RecordSetup::withConfig()`. Both allow setting up secondary container (explained in next
section) access. Look at [`ContainerSetup`](src/Setup/ContainerSetup.php) static constructors
section) access. Look at [`ContainerSetup`](src/Setup.php) static constructors
for details on composition of `RecordCollection` object.


### Secondary Container

Passing [`CombinedRecordCollection`](src/Setup/CombinedRecordCollection.php) to `ContainerSetup`
Passing [`CombinedRecordCollection`](src/Records/CombinedRecordCollection.php) to `ContainerSetup`
or `RecordContainer` constructor instead its base class (`RecordCollection`) allows accessing
Container with different behaviour than default one (using records) when its contents is
retrieved with defined id prefix. You may use (static) named constructor to pass both associated
Expand Down
3 changes: 1 addition & 2 deletions src/RecordContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

namespace Polymorphine\Container;

use Polymorphine\Container\Setup\RecordCollection;
use Psr\Container\ContainerInterface;


class RecordContainer implements ContainerInterface
{
protected $records;

public function __construct(RecordCollection $records)
public function __construct(Records $records)
{
$this->records = $records;
}
Expand Down
59 changes: 59 additions & 0 deletions src/Records.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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;

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


interface Records
{
/**
* Checks if Record is stored at given identifier.
*
* @param string $id
*
* @return bool
*/
public function has(string $id): bool;

/**
* Returns Record stored at given identifier.
*
* @param string $id
* @param ContainerInterface $container
*
* @throws Exception\RecordNotFoundException
*
* @return mixed
*/
public function get(string $id, ContainerInterface $container);

/**
* Stores Record at given $name identifier.
*
* @param $id
* @param Record $record
*
* @throws Exception\InvalidIdException
*/
public function add(string $id, Record $record): void;

/**
* Moves Record to different identifier.
*
* @param string $id
*
* @return string New identifier of moved Record
*/
public function moveRecord(string $id): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,51 @@
* with this source code in the file LICENSE.
*/

namespace Polymorphine\Container\Setup;
namespace Polymorphine\Container\Records;

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


/**
* RecordCollection with secondary Container accessed using prefixed ids.
*/
class CombinedRecordCollection extends RecordCollection
class CombinedRecordCollection implements Records
{
private $records;
private $config;
private $prefix;
private $prefixLength;

/**
* @param Record[] $records
* @param Records $records
* @param ContainerInterface $config
* @param string $prefix
*
* @throws Exception\InvalidArgumentException
*/
public function __construct(array $records, ContainerInterface $config, string $prefix = '.')
public function __construct(Records $records, ContainerInterface $config, string $prefix = '.')
{
$this->config = $config;
$this->prefix = $prefix;
$this->records = $records;
$this->config = $config;
$this->prefix = $prefix;

$this->prefixLength = strlen($prefix);
parent::__construct($records);
}

public function has(string $id): bool
{
return $this->isConfigId($id)
? $this->config->has($this->removePrefix($id))
: parent::has($id);
: $this->records->has($id);
}

public function get(string $id, ContainerInterface $container)
{
return $this->isConfigId($id)
? $this->config->get($this->removePrefix($id))
: parent::get($id, $container);
: $this->records->get($id, $container);
}

public function add(string $id, Record $record): void
Expand All @@ -61,12 +63,12 @@ public function add(string $id, Record $record): void
throw new Exception\InvalidIdException(sprintf($message, $this->prefix));
}

parent::add($id, $record);
$this->records->add($id, $record);
}

public function moveRecord(string $id): string
{
return parent::moveRecord($id);
return $this->records->moveRecord($id);
}

private function isConfigId(string $id): bool
Expand Down
4 changes: 2 additions & 2 deletions src/Setup/Record.php → src/Records/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* with this source code in the file LICENSE.
*/

namespace Polymorphine\Container\Setup;
namespace Polymorphine\Container\Records;

use Psr\Container\ContainerInterface;


/**
* Abstract strategy for retrieving values from RecordContainer
* Abstract strategy for retrieving values from RecordContainer.
*/
interface Record
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* with this source code in the file LICENSE.
*/

namespace Polymorphine\Container\Setup\Record;
namespace Polymorphine\Container\Records\Record;

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* with this source code in the file LICENSE.
*/

namespace Polymorphine\Container\Setup\Record;
namespace Polymorphine\Container\Records\Record;

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* with this source code in the file LICENSE.
*/

namespace Polymorphine\Container\Setup\Record;
namespace Polymorphine\Container\Records\Record;

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* with this source code in the file LICENSE.
*/

namespace Polymorphine\Container\Setup\Record;
namespace Polymorphine\Container\Records\Record;

use Psr\Container\ContainerInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* with this source code in the file LICENSE.
*/

namespace Polymorphine\Container\Setup\Record;
namespace Polymorphine\Container\Records\Record;

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
* with this source code in the file LICENSE.
*/

namespace Polymorphine\Container\Setup;
namespace Polymorphine\Container\Records;

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


class RecordCollection
class RecordCollection implements Records
{
private $records;

Expand All @@ -27,41 +28,16 @@ public function __construct(array $records = [])
$this->records = $records;
}

/**
* Checks if Record is stored at given identifier.
*
* @param string $id
*
* @return bool
*/
public function has(string $id): bool
{
return isset($this->records[$id]);
}

/**
* Returns Record stored at given identifier.
*
* @param string $id
* @param ContainerInterface $container
*
* @throws Exception\RecordNotFoundException
*
* @return mixed
*/
public function get(string $id, ContainerInterface $container)
{
return $this->getRecord($id)->value($container);
}

/**
* Stores Record at given $name identifier.
*
* @param $id
* @param Record $record
*
* @throws Exception\InvalidIdException
*/
public function add(string $id, Record $record): void
{
if (isset($this->records[$id])) {
Expand All @@ -71,13 +47,6 @@ public function add(string $id, Record $record): void
$this->records[$id] = $record;
}

/**
* Moves Record to different identifier.
*
* @param string $id
*
* @return string New identifier of moved Record
*/
public function moveRecord(string $id): string
{
if (!isset($this->records[$id])) {
Expand Down
Loading

0 comments on commit a7efadd

Please sign in to comment.