Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Dependency Injection architecture
- Loading branch information
Showing
10 changed files
with
199 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
namespace CloudObjects\PhpMAE\DI; | ||
|
||
use ML\JsonLD\Node; | ||
use ML\IRI\IRI; | ||
use DI\ContainerBuilder; | ||
use CloudObjects\SDK\ObjectRetriever, CloudObjects\SDK\NodeReader, CloudObjects\SDK\COIDParser; | ||
use CloudObjects\SDK\WebAPI\APIClientFactory; | ||
use CloudObjects\PhpMAE\ObjectRetrieverPool, CloudObjects\PhpMAE\ClassRepository, CloudObjects\PhpMAE\ErrorHandler, CloudObjects\PhpMAE\Engine; | ||
use CloudObjects\PhpMAE\Exceptions\PhpMAEException; | ||
|
||
/** | ||
* The DependencyInjector returns all the dependencies specified for a PHP class. | ||
*/ | ||
class DependencyInjector { | ||
|
||
private $retrieverPool; | ||
private $classRepository; | ||
|
||
public function __construct(ObjectRetrieverPool $retrieverPool, ClassRepository $classRepository) { | ||
$this->retrieverPool = $retrieverPool; | ||
$this->classRepository = $classRepository; | ||
} | ||
|
||
/** | ||
* Get all dependencies for injection. | ||
* | ||
* @param Node $object The object representing the PHP class. | ||
*/ | ||
public function getDependencies(Node $object) { | ||
$reader = new NodeReader([ | ||
'prefixes' => [ 'phpmae' => 'coid://phpmae.cloudobjects.io/' ] | ||
]); | ||
|
||
$dependencies = $reader->getAllValuesNode($object, 'phpmae:hasDependency'); | ||
|
||
$definitions = []; | ||
|
||
foreach ($dependencies as $d) { | ||
$keyedDependency = null; | ||
|
||
if (!$d->getProperty('coid://phpmae.cloudobjects.io/hasKey')) | ||
throw new PhpMAEException("<".$object->getId()."> has an invalid dependency: no key!"); | ||
|
||
if ($reader->hasType($d, 'phpmae:StaticTextDependency')) { | ||
// Static Text Dependency | ||
$value = $reader->getFirstValueString($d, 'phpmae:hasValue'); | ||
if (!isset($value)) | ||
throw new PhpMAEException("<".$object->getId()."> has an invalid dependency: StaticTextDependency without value!"); | ||
|
||
$keyedDependency = $value; | ||
} else | ||
if ($reader->hasType($d, 'phpmae:WebAPIDependency')) { | ||
// Web API Dependency | ||
$apiCoid = $reader->getFirstValueString($d, 'phpmae:hasAPI'); | ||
if (!isset($apiCoid)) | ||
throw new PhpMAEException("<".$object->getId()."> has an invalid dependency: WebAPIDependency without API!"); | ||
|
||
$keyedDependency = function() use ($apiCoid, $retrieverPool, $object) { | ||
$namespaceCoid = COIDParser::getNamespaceCOID(new IRI($object->getId())); | ||
$apiCoid = new IRI($apiCoid); | ||
return APIClientFactory::createClient($this->retrieverPool->getBaseObjectRetriever()->get($apiCoid), | ||
$retrieverPool->getObjectRetriever($apiCoid->getHost())->get($namespaceCoid)); | ||
}; | ||
} else | ||
if ($reader->hasType($d, 'phpmae:ClassDependency')) { | ||
// Class Dependency | ||
$classCoid = $reader->getFirstValueIRI($d, 'phpmae:hasClass'); | ||
if (!isset($classCoid)) | ||
throw new PhpMAEException("<".$object->getId()."> has an invalid dependency: ClassDependency without class!"); | ||
|
||
$classRepository = $this->classRepository; | ||
$keyedDependency = function() use ($classRepository, $classCoid) { | ||
$dependencyContainer = $classRepository->createInstance($this->retrieverPool->getBaseObjectRetriever()->getObject($classCoid), | ||
$this->retrieverPool->getBaseObjectRetriever(), new ErrorHandler); | ||
return $dependencyContainer->get(Engine::SKEY); | ||
}; | ||
} else | ||
throw new PhpMAEException("<".$object->getId()."> has an invalid dependency: unknown type!"); | ||
|
||
$definitions[$reader->getFirstValueString($d, 'phpmae:hasKey')] = $keyedDependency; | ||
} | ||
|
||
return $definitions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
namespace CloudObjects\PhpMAE\DI; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* The SandboxedContainer is a proxy class for a PSR-11 container that | ||
* ensures that only interface methods are available. | ||
*/ | ||
class SandboxedContainer implements ContainerInterface { | ||
|
||
private $container; | ||
|
||
public function __construct(ContainerInterface $container) { | ||
$this->container = $container; | ||
} | ||
|
||
public function has($id) { | ||
return $this->container->has($id); | ||
} | ||
|
||
public function get($id) { | ||
return $this->container->get($id); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
namespace CloudObjects\PhpMAE\DI; | ||
|
||
use DI\Definition\ObjectDefinition; | ||
use DI\Definition\Source\DefinitionSource, DI\Definition\Source\Autowiring, | ||
DI\Definition\Source\ReflectionBasedAutowiring; | ||
|
||
/** | ||
* This is an extension of ReflectionBasedAutowiring that only allows autowiring | ||
* for classes that have a definition. Used for sandboxing containers. | ||
*/ | ||
class WhitelistReflectionBasedAutowiring extends ReflectionBasedAutowiring implements DefinitionSource, Autowiring { | ||
|
||
public function autowire(string $name, ObjectDefinition $definition = null) { | ||
return isset($definition) ? parent::autowire($name, $definition) : null; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ public function reply($data) { | |
->write($data); | ||
} | ||
|
||
|
||
/** | ||
* Get the response | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.