Skip to content

Commit

Permalink
Merge pull request #47 from chrismichaels84/develop
Browse files Browse the repository at this point in the history
Merge Develop into Master for v0.9
  • Loading branch information
Michael Wilson committed Aug 22, 2016
2 parents 52615e3 + 19a6439 commit 47bf267
Show file tree
Hide file tree
Showing 27 changed files with 163 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,10 @@ All Notable changes to `Manager` will be documented in this file
- Move to a new testing structure
- Add integration tests
- Cleanup and refactor
- add `has('$dep.whatever')` interpolation
- add linking (multiple aliases) to ioc manager
- add to ioc manager, fetch a class by string
- Allow user to force no namespacing in file loading

# v0.8.9 - 3-11-2016
- Add full composer.json
Expand Down
16 changes: 16 additions & 0 deletions ioc.md
Expand Up @@ -55,6 +55,19 @@ When you're ready to call dependencies:
$manager->fetch('event_dispatcher');
```

You may also register multiple aliases to a single dependency
```php
$manager->di(['one', 'two', 'three'], $factory);
$manager->fetch('one');
$manager->fetch('two');
$manager->fetch('three'); // All the same
```

And, just ask for a class. If it exists (and nothing by that name was registered), it will be loaded.
```php
$manager->fetch('Some/Class')
```

## Dependencies that need Dependencies
The easiest way to setup a dependency that needs a dependency is to use a closure.
```php
Expand Down Expand Up @@ -125,4 +138,7 @@ If you pass a value that is not a registered dependency, then the value itself i

NOTE: For the moment, you cannot prepare dependencies that are instances of containers.


### Has()
If you want to check if a dependency exists, use `$manager->has('$dep.whatever')`. Not the single quotes.
Any feedback here would be appreciated. Take a look at `IocManagerInterface` for future plans.
2 changes: 1 addition & 1 deletion src/Bags/FileBag.php
Expand Up @@ -61,7 +61,7 @@ protected function initialize(array $splFileInfoObjects = [])
/**
* Check for an \SplFileInfo object or a custom namespaces object
* @param $entity
* @return bool
* @return \SplFileInfo|\SplFileInfo[]|string
*/
protected function createSplFileInfoObject($entity)
{
Expand Down
6 changes: 0 additions & 6 deletions src/BasicManager.php
Expand Up @@ -19,12 +19,6 @@ class BasicManager implements
{
use ManagesItemsTrait;

/**
* The items stored in the manager
* @var array $items Items governed by manager
*/
protected $items;

/**
* Build a new manager instance
* @param array $items
Expand Down
6 changes: 0 additions & 6 deletions src/ConfigManager.php
Expand Up @@ -31,12 +31,6 @@ class ConfigManager implements
{
use ManagesItemsTrait, ArrayableTrait, LoadsFilesTrait;

/**
* The items stored in the manager
* @var array $items Items governed by manager
*/
protected $items;

/**
* Build a new manager instance
* @param array $items
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/IocContainerInterface.php
Expand Up @@ -13,7 +13,7 @@ interface IocContainerInterface
* Returns the request object with all dependencies
*
* @param string $alias
* @return object
* @return mixed
*/
public function fetch($alias);
}
1 change: 1 addition & 0 deletions src/Contracts/IocManagerInterface.php
Expand Up @@ -40,6 +40,7 @@ public function share($alias);
* Add a pipeline to to the que for a specific item
* @param $alias
* @param $pipeline
* @return void
*/
public function setup($alias, $pipeline);

Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/ManagesItemsInterface.php
Expand Up @@ -53,11 +53,11 @@ public function set($alias, $item = null);
* Push a value or values onto the end of an array inside manager
* @param string $alias The level of nested data
* @param mixed $value The first value to append
* @param null|mixed $_ Optional other values to apend
* @param null|mixed $other Optional other values to amend
* @return int Number of items pushed
* @throws ItemNotFoundException If pushing to unset array
*/
public function push($alias, $value, $_ = null);
public function push($alias, $value, $other = null);

/**
* Get a single item
Expand Down
2 changes: 1 addition & 1 deletion src/Decoders/JsonDecoder.php
Expand Up @@ -36,7 +36,7 @@ public function decode($data)

/**
* Returns MimeType
* @return string
* @return string[]
*/
public function getMimeType()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Decoders/YamlDecoder.php
Expand Up @@ -22,7 +22,7 @@ class YamlDecoder implements DecoderInterface
public function decode($data)
{
$parser = new Parser();
$this->arrayData = $parser->parse($data);
$this->arrayData = (array) $parser->parse($data);
return $this->arrayData;
}

Expand Down
19 changes: 12 additions & 7 deletions src/FileLoader.php
Expand Up @@ -74,7 +74,7 @@ public function addDecoder(DecoderInterface $decoder)
*/
public function addFiles($files)
{
if ($files instanceof Filebag) {
if ($files instanceof FileBag) {
$this->fileBag = $files;
return;
}
Expand All @@ -89,23 +89,24 @@ public function addFiles($files)

/**
* Process the current FileBag and return an array
* @param bool $ns
* @return array
* @throws Exception
*/
public function process()
public function process($ns = true)
{
return $this->decodedData = $this->decodeFileBagData($this->fileBag);
return $this->decodedData = $this->decodeFileBagData($this->fileBag, $ns);
}

/**
* Process file bag to load into the data manager.
* A file bag is an array of SplFileInfo objects.
*
* @param array|FileBag $fileBag
* @param bool $ns
* @return array
* @throws Exception
*/
public function decodeFileBagData(FileBag $fileBag)
public function decodeFileBagData(FileBag $fileBag, $ns = true)
{
$decodedData = [];
$files = $fileBag->getAllFileInfoObjects();
Expand All @@ -120,7 +121,11 @@ public function decodeFileBagData(FileBag $fileBag)
$fileData = $this->decodeFile($file);
if ($fileData) {
foreach ($fileData as $k => $v) {
$decodedData[$namespace][$k] = $v;
if ($ns === true) {
$decodedData[$namespace][$k] = $v;
} else {
$decodedData[$k] = $v;
}
}
}
}
Expand Down Expand Up @@ -189,7 +194,7 @@ protected function checkAndAddDefaultDecoder($mimeType)
/**
* Decodes a single file using registered decoders
* @param \SplFileInfo $file
* @return null
* @return string
*/
protected function decodeFile($file)
{
Expand Down
6 changes: 0 additions & 6 deletions src/Manager.php
Expand Up @@ -31,12 +31,6 @@ class Manager implements
{
use ManagesItemsTrait, ChainsNestedItemsTrait, ArrayableTrait;

/**
* The items stored in the manager
* @var array $items Items governed by manager
*/
protected $items;

/**
* Build a new manager instance
* @param array $items
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/ArrayableTrait.php
Expand Up @@ -68,7 +68,7 @@ public function count()

/**
* @implements JSONSerializable
* @return string
* @return array
*/
public function jsonSerialize()
{
Expand Down
5 changes: 3 additions & 2 deletions src/Traits/CollectionTrait.php
Expand Up @@ -10,6 +10,7 @@
* MUST be used with ManagesItemsTrait
*
* @implements Michaels\Manager\Contracts\ChainsNestedItemsInterface
* @method addToChain() from ChainsNestedItems
* @package Michaels\Manager
*/
trait CollectionTrait
Expand All @@ -30,7 +31,7 @@ trait CollectionTrait
/**
* Converts an array to a collection if value is arrayable and config is set
* @param $value
* @return static
* @return ArrayImitator
*/
public function toCollection($value)
{
Expand Down Expand Up @@ -161,8 +162,8 @@ protected function callCollectionMethod($method, $arguments, $instance, $flag, $
$return = $this;
break;

default:
case (static::$RETURN_ARRAY):
default:
$return = $value->toArray();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Traits/DependsOnManagesItemsTrait.php
Expand Up @@ -48,11 +48,11 @@ abstract public function set($alias, $item = null);
* Push a value or values onto the end of an array inside manager
* @param string $alias The level of nested data
* @param mixed $value The first value to append
* @param null|mixed $_ Optional other values to apend
* @param null|mixed $other Optional other values to apend
* @return int Number of items pushed
* @throws ItemNotFoundException If pushing to unset array
*/
abstract public function push($alias, $value, $_ = null);
abstract public function push($alias, $value, $other = null);

/**
* Get a single item
Expand Down
11 changes: 6 additions & 5 deletions src/Traits/LoadsFilesTrait.php
Expand Up @@ -38,19 +38,20 @@ protected function initializeFileLoader(FileLoader $fileLoader = null)
*
* @param array $files an array of SplFileInfo Objects
* @param $append boolean true, if data should be appended to the manager.
* @return array
* @param bool $namespace
* @return void
*/
public function loadFiles(array $files, $append = false)
public function loadFiles(array $files, $append = false, $namespace = true)
{
$this->initializeFileLoader();
$this->fileLoader->addFiles($files);
$data = $this->fileLoader->process();
$data = $this->fileLoader->process($namespace);
$this->hydrate($data, $append);
}

public function loadFile($file, $append = false)
public function loadFile($file, $append = false, $namespace = true)
{
return $this->loadFiles([$file], $append);
$this->loadFiles([$file], $append, $namespace);
}

/**
Expand Down
29 changes: 26 additions & 3 deletions src/Traits/ManagesIocTrait.php
Expand Up @@ -33,7 +33,7 @@ public function initDi(array $components = [])

/**
* Returns the entire IoC Manifest
* @return array
* @return array|NoItemFoundMessage
*/
public function getIocManifest()
{
Expand All @@ -51,11 +51,18 @@ public function getIocManifest()
*
* @param string $alias
* @param string|mixed $fallback
* @return object
* @return mixed
* @throws \Exception
*/
public function fetch($alias, $fallback = '_michaels_no_fallback')
{
// If this is a link, just go back to the master
$link = $this->getIfExists($this->nameOfIocManifest . ".$alias");
if (is_string($link) && strpos($link, '_michaels_link_') !== false) {
return $this->fetch(str_replace('_michaels_link_', '', $link));
}

// Otherwise, continue
$shared = $this->getIfExists($this->nameOfIocManifest . "._singletons.$alias");

if ($shared instanceof NoItemFoundMessage) {
Expand All @@ -66,7 +73,7 @@ public function fetch($alias, $fallback = '_michaels_no_fallback')
if (is_object($shared)) {
return $shared;

// This is shared, but we must produce and cache it
// This is shared, but we must produce and cache it
} else {
$object = $this->produceDependency($alias, $fallback);
$this->set($this->nameOfIocManifest . "._singletons.$alias", $object);
Expand All @@ -90,13 +97,27 @@ public function fetch($alias, $fallback = '_michaels_no_fallback')
*/
public function di($alias, $factory, array $declared = null)
{
// Setup links, if necessary
if (is_array($alias)) {
$links = $alias;
$alias = $alias[0];
unset($links[0]);
}

$this->set($this->nameOfIocManifest . ".$alias", $factory);

// Setup any declared dependencies
if ($declared) {
$this->set($this->nameOfIocManifest . "._declarations.$alias", $declared);
}

// Add Links
if (!empty($links)) {
foreach ($links as $link) {
$this->set($this->nameOfIocManifest . ".$link", "_michaels_link_$alias");
}
}

return $this;
}

Expand Down Expand Up @@ -160,6 +181,8 @@ protected function produceDependency($alias, $fallback = '_michaels_no_fallback'
if ($factory instanceof NoItemFoundMessage) {
if ($fallback !== '_michaels_no_fallback') {
return $fallback;
} elseif (class_exists($alias)) {
return new $alias;
} else {
throw new ItemNotFoundException("$alias not found");
}
Expand Down
11 changes: 8 additions & 3 deletions src/Traits/ManagesItemsTrait.php
Expand Up @@ -147,13 +147,13 @@ public function set($alias, $item = null)
* Push a value or values onto the end of an array inside manager
* @param string $alias The level of nested data
* @param mixed $value The first value to append
* @param null|mixed $_ Optional other values to apend
* @param null|mixed $other Optional other values to apend
* @return int Number of items pushed
* @throws ItemNotFoundException If pushing to unset array
*/
public function push($alias, $value, $_ = null)
public function push($alias, $value, $other = null)
{
if (isset($_)) {
if (isset($other)) {
$values = func_get_args();
array_shift($values);
} else {
Expand Down Expand Up @@ -258,6 +258,11 @@ public function all()
*/
public function exists($alias)
{
// If we are looking for a dependency
if (strpos($alias, '$dep.') !== false) {
$alias = str_replace('$dep', $this->getDiItemsName(), $alias);
}

$repo = $this->getItemsName();
$loc = &$this->$repo;
foreach (explode('.', $alias) as $step) {
Expand Down
7 changes: 0 additions & 7 deletions src/UberManager.php
Expand Up @@ -38,16 +38,9 @@ class UberManager implements

// Standards
ContainerInterface

{
use ManagesItemsTrait, ChainsNestedItemsTrait, ArrayableTrait, ManagesIocTrait, LoadsFilesTrait;

/**
* The items stored in the manager
* @var array $items Items governed by manager
*/
protected $items;

/**
* Build a new manager instance
* @param array $items
Expand Down

0 comments on commit 47bf267

Please sign in to comment.