Commit 9452a158 authored by Josh Pollock's avatar Josh Pollock
Browse files

copy code from interop

parents
<?php
namespace calderawp\CalderaContainers\Exceptions;
/**
* Class Exception
*
* Generic Exception - All exceptions from this library MUST be this or a subclass of this.
*/
class Exception extends \Exception
{
}
<?php
namespace calderawp\CalderaContainers\Exceptions;
/**
* Class NotImplemented
*
* Exception to use for any container services that are expected to be provided, but not yet provided
*/
class NotImplemented extends Exception
{
}
<?php
namespace calderawp\CalderaContainers\Interfaces;
interface Arrayable
{
/**
* @return array
*/
public function toArray();
}
<?php
namespace calderawp\CalderaContainers\Interfaces;
/**
* Interface ContainsServices
*
* Interface that any object that contains services, for example a service container, MUST implement.
*/
interface ContainsServices
{
}
<?php
namespace calderawp\interop\Interfaces;
use calderawp\CalderaContainers\Interfaces\Arrayable;
/**
* Interface JsonArrayable
*
* Interface that all objects that covnert to an array that is then used to convert to JSON MUST Impliment
*/
interface JsonArrayable extends Arrayable, \JsonSerializable
{
}
<?php
namespace calderawp\CalderaContainers\Interfaces;
use calderawp\CalderaContainers\Container;
/**
* Interface ProvidesService
*
* Interface that all service providers MUST implement
*
*/
interface ProvidesService
{
/**
* Register provider
*
* @param Container $container
*/
public function registerService(Container $container);
}
<?php
namespace calderawp\CalderaContainers\Interfaces;
/**
* Interface ServiceContainer
*
* Interface that all Service Containers MUST implement
*/
interface ServiceContainer
{
/**
* Check if container provides a service
*
* @param string $serviceName Service name, should be ::class reference
* @return bool
*/
public function doesProvide($serviceName);
/**
* Bind a service to the container.
*
* @param string $alias Alias for class
* @param object|\Closure $concrete Concrete class
co */
public function bind($alias, $concrete);
/**
* Request a service from the container.
*
* @param string $alias Alias for class
* @return object|mixed
*/
public function make($alias);
/**
* Bind a singleton instance to the container.
*
* @param string $alias Alias for class
* @param object $binding Single instance of object to bind
*/
public function singleton($alias, $binding);
}
<?php
namespace calderawp\CalderaContainers\Service;
use calderawp\CalderaContainers\Interfaces\ProvidesService;
/**
* Class Container
*
* Primary service container.
*
* This is based on WP Pusher's service container.
* Cool plugin for managin plugin/ theme installs with git.
* -> https://wppusher.com/
*/
class Container
{
/**
* @var ProvidesService[]
*/
protected $services;
/** @inheritdoc */
public function doesProvide($serviceName)
{
if (! is_array($this->services)) {
$this->services = [];
}
return ! empty($this->services) && array_key_exists($serviceName, $this->services);
}
/** @inheritdoc */
public function bind($alias, $concrete)
{
$this->services[$alias] = $concrete;
}
/** @inheritdoc */
public function make($alias)
{
if (! isset($this->services[$alias])) {
return $this->resolve($alias);
}
if (is_callable($this->services[$alias])) {
return call_user_func_array($this->services[$alias], array($this));
}
if (is_object($this->services[$alias])) {
return $this->services[$alias];
}
if (class_exists($this->services[$alias])) {
return $this->resolve($this->services[$alias]);
}
return $this->resolve($alias);
}
/** @inheritdoc */
public function singleton($alias, $binding)
{
$this->services[$alias] = $binding;
}
/**
* Resolve dependencies.
*
* @todo use Doctrine Insanitator?
*
* @param $class
* @return object
*/
private function resolve($class)
{
$reflection = new \ReflectionClass($class);
$constructor = $reflection->getConstructor();
// Constructor is null
if (! $constructor) {
return new $class;
}
// Constructor with no parameters
$params = $constructor->getParameters();
if (count($params) === 0) {
return new $class;
}
$newInstanceParams = array();
foreach ($params as $param) {
if (is_null($param->getClass())) {
$newInstanceParams[] = null;
continue;
}
$newInstanceParams[] = $this->make(
$param->getClass()->getName()
);
}
return $reflection->newInstanceArgs(
$newInstanceParams
);
}
}
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