Commit 32ba0d0e authored by Josh Pollock's avatar Josh Pollock
Browse files

readme for service container

parent 45bc7fe9
......@@ -3,9 +3,9 @@
<component name="ChangeListManager">
<list default="true" id="39e46151-e7c1-422c-b495-7bbe1c526bbf" name="Default" comment="">
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/README.MD" afterPath="$PROJECT_DIR$/README.MD" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/Tests/Mocks/Provider.php" afterPath="$PROJECT_DIR$/Tests/Mocks/Provider.php" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/Tests/ServiceServiceContainerTest.php" afterPath="$PROJECT_DIR$/Tests/ServiceServiceContainerTest.php" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/composer.json" afterPath="$PROJECT_DIR$/composer.json" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/src/Service/Container.php" afterPath="$PROJECT_DIR$/src/Service/Container.php" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="TRACKING_ENABLED" value="true" />
......@@ -16,22 +16,22 @@
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file leaf-file-name="bootstrap.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Tests/bootstrap.php">
<file leaf-file-name="ServiceServiceContainerTest.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Tests/ServiceServiceContainerTest.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="300">
<caret line="20" column="0" lean-forward="false" selection-start-line="20" selection-start-column="0" selection-end-line="20" selection-end-column="0" />
<state relative-caret-position="188">
<caret line="49" column="68" lean-forward="false" selection-start-line="49" selection-start-column="68" selection-end-line="49" selection-end-column="68" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="README.MD" pinned="false" current-in-tab="false">
<file leaf-file-name="README.MD" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/README.MD">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor relative-caret-position="210">
<caret line="14" column="7" lean-forward="false" selection-start-line="14" selection-start-column="7" selection-end-line="14" selection-end-column="7" />
<first_editor relative-caret-position="278">
<caret line="75" column="14" lean-forward="false" selection-start-line="75" selection-start-column="14" selection-end-line="75" selection-end-column="14" />
<folding />
</first_editor>
<second_editor />
......@@ -39,43 +39,11 @@
</provider>
</entry>
</file>
<file leaf-file-name="composer.json" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/composer.json">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="191">
<caret line="28" column="35" lean-forward="false" selection-start-line="28" selection-start-column="35" selection-end-line="28" selection-end-column="35" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="Container.php" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/Service/Container.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="37">
<caret line="23" column="37" lean-forward="false" selection-start-line="23" selection-start-column="37" selection-end-line="23" selection-end-column="37" />
<folding>
<element signature="e#56#115#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="ServiceServiceContainerTest.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Tests/ServiceServiceContainerTest.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="286">
<caret line="94" column="25" lean-forward="false" selection-start-line="94" selection-start-column="25" selection-end-line="94" selection-end-column="25" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="ProvidesService.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/Interfaces/ProvidesService.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="60">
<caret line="4" column="0" lean-forward="false" selection-start-line="4" selection-start-column="0" selection-end-line="4" selection-end-column="0" />
<state relative-caret-position="195">
<caret line="13" column="7" lean-forward="true" selection-start-line="13" selection-start-column="7" selection-end-line="13" selection-end-column="7" />
<folding />
</state>
</provider>
......@@ -107,6 +75,7 @@
<findStrings>
<find>\calderawp\interop\</find>
<find>$container = new \calderawp\interop\Service\Container();</find>
<find>pro</find>
</findStrings>
<replaceStrings>
<replace>\calderawp\CalderaContainers\</replace>
......@@ -137,11 +106,13 @@
<option value="$PROJECT_DIR$/src/Exceptions/NotFoundException.php" />
<option value="$PROJECT_DIR$/src/Exceptions/Exception.php" />
<option value="$PROJECT_DIR$/src/Container.php" />
<option value="$PROJECT_DIR$/README.MD" />
<option value="$PROJECT_DIR$/src/Interfaces/ProvidesService.php" />
<option value="$PROJECT_DIR$/composer.json" />
<option value="$PROJECT_DIR$/Tests/ServiceServiceContainerTest.php" />
<option value="$PROJECT_DIR$/src/Service/Container.php" />
<option value="$PROJECT_DIR$/Tests/Mocks/SivanService.php" />
<option value="$PROJECT_DIR$/Tests/Mocks/Provider.php" />
<option value="$PROJECT_DIR$/Tests/ServiceServiceContainerTest.php" />
<option value="$PROJECT_DIR$/README.MD" />
</list>
</option>
</component>
......@@ -183,11 +154,34 @@
<item name="caldera-containers" type="b2602c69:ProjectViewProjectNode" />
<item name="caldera-containers" type="2a2b976b:PhpTreeStructureProvider$1" />
</path>
<path>
<item name="caldera-containers" type="b2602c69:ProjectViewProjectNode" />
<item name="caldera-containers" type="2a2b976b:PhpTreeStructureProvider$1" />
<item name="src" type="2a2b976b:PhpTreeStructureProvider$1" />
</path>
<path>
<item name="caldera-containers" type="b2602c69:ProjectViewProjectNode" />
<item name="caldera-containers" type="2a2b976b:PhpTreeStructureProvider$1" />
<item name="src" type="2a2b976b:PhpTreeStructureProvider$1" />
<item name="Interfaces" type="2a2b976b:PhpTreeStructureProvider$1" />
</path>
<path>
<item name="caldera-containers" type="b2602c69:ProjectViewProjectNode" />
<item name="caldera-containers" type="2a2b976b:PhpTreeStructureProvider$1" />
<item name="src" type="2a2b976b:PhpTreeStructureProvider$1" />
<item name="Service" type="2a2b976b:PhpTreeStructureProvider$1" />
</path>
<path>
<item name="caldera-containers" type="b2602c69:ProjectViewProjectNode" />
<item name="caldera-containers" type="2a2b976b:PhpTreeStructureProvider$1" />
<item name="Tests" type="2a2b976b:PhpTreeStructureProvider$1" />
</path>
<path>
<item name="caldera-containers" type="b2602c69:ProjectViewProjectNode" />
<item name="caldera-containers" type="2a2b976b:PhpTreeStructureProvider$1" />
<item name="Tests" type="2a2b976b:PhpTreeStructureProvider$1" />
<item name="Mocks" type="2a2b976b:PhpTreeStructureProvider$1" />
</path>
</expand>
<select />
</subPane>
......@@ -232,12 +226,12 @@
<option name="presentableId" value="Default" />
<updated>1522953897768</updated>
<workItem from="1522953898944" duration="2929000" />
<workItem from="1525558486917" duration="552000" />
<workItem from="1525558486917" duration="1693000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="3481000" />
<option name="totallyTimeSpent" value="4622000" />
</component>
<component name="ToolWindowManager">
<frame x="144" y="22" width="1296" height="878" extended-state="0" />
......@@ -349,6 +343,7 @@
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="150">
<caret line="10" column="106" lean-forward="false" selection-start-line="10" selection-start-column="106" selection-end-line="10" selection-end-column="106" />
<folding />
</state>
</provider>
</entry>
......@@ -356,6 +351,7 @@
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="195">
<caret line="13" column="1" lean-forward="false" selection-start-line="13" selection-start-column="1" selection-end-line="13" selection-end-column="1" />
<folding />
</state>
</provider>
</entry>
......@@ -364,6 +360,7 @@
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="105">
<caret line="7" column="62" lean-forward="false" selection-start-line="7" selection-start-column="62" selection-end-line="7" selection-end-column="62" />
<folding />
</state>
</provider>
</entry>
......@@ -372,6 +369,7 @@
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="45">
<caret line="3" column="24" lean-forward="false" selection-start-line="3" selection-start-column="0" selection-end-line="4" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
......@@ -395,6 +393,7 @@
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="135">
<caret line="9" column="17" lean-forward="false" selection-start-line="9" selection-start-column="17" selection-end-line="9" selection-end-column="17" />
<folding />
</state>
</provider>
</entry>
......@@ -402,6 +401,7 @@
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="59">
<caret line="258" column="6" lean-forward="false" selection-start-line="258" selection-start-column="6" selection-end-line="258" selection-end-column="6" />
<folding />
</state>
</provider>
</entry>
......@@ -436,14 +436,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Tests/bootstrap.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="300">
<caret line="20" column="0" lean-forward="false" selection-start-line="20" selection-start-column="0" selection-end-line="20" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Container.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="225">
......@@ -455,17 +447,7 @@
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165">
<caret line="11" column="34" lean-forward="false" selection-start-line="11" selection-start-column="15" selection-end-line="11" selection-end-column="34" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/README.MD">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor relative-caret-position="210">
<caret line="14" column="7" lean-forward="false" selection-start-line="14" selection-start-column="7" selection-end-line="14" selection-end-column="7" />
<folding />
</first_editor>
<second_editor />
</state>
</provider>
</entry>
......@@ -479,12 +461,30 @@
</entry>
<entry file="file://$PROJECT_DIR$/src/Interfaces/ProvidesService.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="60">
<caret line="4" column="0" lean-forward="false" selection-start-line="4" selection-start-column="0" selection-end-line="4" selection-end-column="0" />
<state relative-caret-position="195">
<caret line="13" column="7" lean-forward="true" selection-start-line="13" selection-start-column="7" selection-end-line="13" selection-end-column="7" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Tests/bootstrap.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="300">
<caret line="20" column="0" lean-forward="false" selection-start-line="20" selection-start-column="0" selection-end-line="20" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Service/Container.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="158">
<caret line="47" column="20" lean-forward="false" selection-start-line="47" selection-start-column="20" selection-end-line="47" selection-end-column="20" />
<folding>
<element signature="e#56#115#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/composer.json">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="191">
......@@ -493,21 +493,32 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Tests/Mocks/Provider.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="225">
<caret line="15" column="24" lean-forward="false" selection-start-line="15" selection-start-column="20" selection-end-line="15" selection-end-column="24" />
<folding>
<element signature="e#61#120#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Tests/ServiceServiceContainerTest.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="286">
<caret line="94" column="25" lean-forward="false" selection-start-line="94" selection-start-column="25" selection-end-line="94" selection-end-column="25" />
<state relative-caret-position="188">
<caret line="49" column="68" lean-forward="false" selection-start-line="49" selection-start-column="68" selection-end-line="49" selection-end-column="68" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Service/Container.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="37">
<caret line="23" column="37" lean-forward="false" selection-start-line="23" selection-start-column="37" selection-end-line="23" selection-end-column="37" />
<folding>
<element signature="e#56#115#0#PHP" expanded="true" />
</folding>
<entry file="file://$PROJECT_DIR$/README.MD">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor relative-caret-position="278">
<caret line="75" column="14" lean-forward="false" selection-start-line="75" selection-start-column="14" selection-end-line="75" selection-end-column="14" />
<folding />
</first_editor>
<second_editor />
</state>
</provider>
</entry>
......
......@@ -18,7 +18,64 @@ Basic PSR-11 compatible container decorating Pimple.
Extends the base container but only allows in specified attributes.
- Is abstract
* `calderawp\CalderaContainers\Service\Coantainer`
A basic service container.
A basic service container, with provider bindings, lazy-loaded objects, and singletons.
### Usage
#### `calderawp\CalderaContainers\Container`
#### `calderawp\CalderaContainers\ControlledContainer`
#### `calderawp\CalderaContainers\Service\Coantainer`
#### Binding As Factory
Add a binding that returns a _new_ object of the same class using the alias `std`
```php
$container = new \calderawp\CalderaContainers\Service\Container();
$container->bind( 'std', function (){
$obj = new \stdClass();
$obj->foo = rand();
return $obj;
});
//$obj1->foo !== $obj2->foo
$obj1 = $container->make('std');
$obj2 = $container->make('std');
```
#### Binding A Singleton
Add a binding that returns a _the same_ object of the same class using the alias `std`. You MUST instantiate class before binding.
```php
$container = new \calderawp\CalderaContainers\Service\Container();
$obj = new \stdClass();
$obj->foo = rand();
$container->singleton( 'std', $obj );
//$obj1->foo === $obj2->foo
$obj1 = $container->make('std');
$obj2 = $container->make('std');
```
#### Binding A Lazy-Loaded Singleton
Add a binding that returns a _the same_ object of the same class using the alias `std`. Class is instantiated 1 times, but is not instantiated until used, if ever.
```php
$container = new \calderawp\CalderaContainers\Service\Container();
$container->singleton( 'std', function (){
$obj = new \stdClass();
$obj->foo = rand();
return $obj;
});
//$obj1->foo === $obj2->foo
$obj1 = $container->make('std');
$obj2 = $container->make('std');
## Stuff.
Copyright 2018 CalderaWP LLC. License: GPL v2 or later.
......@@ -7,7 +7,7 @@ namespace calderawp\CalderaContainers\Tests\Mocks;
use calderawp\CalderaContainers\Interfaces\ProvidesService;
use calderawp\CalderaContainers\Interfaces\ServiceContainer;
class SivanService implements ProvidesService
class Provider implements ProvidesService
{
/** @inheritdoc */
......@@ -15,8 +15,8 @@ class SivanService implements ProvidesService
{
$container->bind( $this->getAlias(), function (){
return (object) [
'Roy',
'Mike'
'Roy' => 'Sivan',
'Mike' => 'Corkum'
];
} );
}
......
......@@ -43,6 +43,24 @@ class ServiceContainerTest extends TestCase
$this->assertSame( $classRef2, get_class( $container->make($classRef2 )) );
}
/**
* Test using a service provider class
*
* @covers \calderawp\CalderaContainers\Interfaces\ProvidesService::registerService()
* @covers \calderawp\CalderaContainers\ServiceContainer::bind()
*/
public function testProvidesService()
{
$container = new \calderawp\CalderaContainers\Service\Container();
$provider = new \calderawp\CalderaContainers\Tests\Mocks\Provider();
$provider->registerService($container);
$providedData = $container->make( $provider->getAlias() );
$this->assertObjectHasAttribute( 'Roy',$providedData );
$this->assertObjectHasAttribute( 'Mike',$providedData );
$this->assertSame( $providedData->Mike, 'Corkum' );
$this->assertSame( $providedData->Roy, 'Sivan' );
}
/**
* Test that each object returned by bind, that is not set to be a singleton
*
......@@ -93,6 +111,7 @@ class ServiceContainerTest extends TestCase
$x->sivan = 'Roy';
return $x;
});
$this->assertSame( $container->make('X'), $container->make('X' ) );
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment