Last active
April 24, 2017 09:32
-
-
Save SerafimArts/a39216cb90433528839af2291621cb46 to your computer and use it in GitHub Desktop.
Laravel Container for Symfony
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
<?php | |
/** | |
* Add this code into app/AppKernel.php | |
*/ | |
class AppKernel extends Kernel | |
{ | |
// .... | |
/** | |
* @return string | |
*/ | |
final protected function getContainerBaseClass() | |
{ | |
return ContainerBridge::class; | |
} | |
// .... | |
} |
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
<?php | |
use Symfony\Component\HttpKernel\Kernel; | |
use Illuminate\Container\Container as ProxyContainer; | |
use Illuminate\Contracts\Container\BindingResolutionException; | |
use Illuminate\Contracts\Container\Container as ProxyContainerInterface; | |
use Symfony\Component\DependencyInjection\Container as SymfonyContainer; | |
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | |
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | |
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; | |
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface; | |
/** | |
* Class ContainerBridge | |
* @package AppBundle\Classes | |
*/ | |
class ContainerBridge extends SymfonyContainer implements ProxyContainerInterface | |
{ | |
/** | |
* @var ProxyContainerInterface | |
*/ | |
private $proxy = null; | |
/** | |
* @param string $id | |
* @param int $invalidBehavior | |
* @return mixed|object | |
* @throws \Exception|ServiceCircularReferenceException|ServiceNotFoundException|InvalidArgumentException|BindingResolutionException | |
*/ | |
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) | |
{ | |
if (!$this->has($id)) { | |
try { | |
return $this->make($id); | |
} catch (BindingResolutionException $e) { | |
switch ($invalidBehavior) { | |
case self::NULL_ON_INVALID_REFERENCE: | |
case self::IGNORE_ON_INVALID_REFERENCE: | |
return null; | |
case self::EXCEPTION_ON_INVALID_REFERENCE: | |
default: | |
throw $e; | |
} | |
} | |
} | |
return parent::get($id, $invalidBehavior); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function make($abstract, array $parameters = []) | |
{ | |
return $this->getProxy()->make($abstract, $parameters); | |
} | |
/** | |
* @return ProxyContainerInterface | |
* @throws \Exception | |
*/ | |
private function getProxy() | |
{ | |
if ($this->proxy === null) { | |
$this->proxy = $this->createProxy(); | |
} | |
return $this->proxy; | |
} | |
/** | |
* @return ProxyContainer | |
* @throws \Exception | |
*/ | |
private function createProxy() | |
{ | |
$proxy = new ProxyContainer(); | |
$proxy->instance(SymfonyContainerInterface::class, $this); | |
$proxy->alias(SymfonyContainerInterface::class, ContainerBridge::class); | |
$proxy->alias(SymfonyContainerInterface::class, SymfonyContainer::class); | |
$proxy->alias(SymfonyContainerInterface::class, ProxyContainer::class); | |
$proxy->alias(SymfonyContainerInterface::class, ProxyContainerInterface::class); | |
$proxy->instance(Kernel::class, $this->get('kernel')); | |
foreach ($this->services as $alias => $instance) { | |
$proxy->bind(get_class($instance), function () use ($alias) { | |
return $this->get($alias); | |
}); | |
} | |
return $proxy; | |
} | |
/** | |
* @return array | |
*/ | |
public function getServices() | |
{ | |
return $this->services; | |
} | |
/** | |
* @return array | |
*/ | |
public function getParameters() | |
{ | |
$resolver = function () { | |
return $this->parameters; | |
}; | |
return call_user_func($resolver->bindTo($this, get_class($this))); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function bound($abstract) | |
{ | |
return $this->getProxy()->bound($abstract); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function alias($abstract, $alias) | |
{ | |
$this->getProxy()->alias($abstract, $alias); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function tag($abstracts, $tags) | |
{ | |
$this->getProxy()->tag($abstracts, $tags); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function tagged($tag) | |
{ | |
$this->getProxy()->tagged($tag); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function bind($abstract, $concrete = null, $shared = false) | |
{ | |
$this->getProxy()->bind($abstract, $concrete, $shared); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function bindIf($abstract, $concrete = null, $shared = false) | |
{ | |
$this->getProxy()->bindIf($abstract, $concrete, $shared); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function singleton($abstract, $concrete = null) | |
{ | |
$this->getProxy()->singleton($abstract, $concrete); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function extend($abstract, \Closure $closure) | |
{ | |
$this->getProxy()->extend($abstract, $closure); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function instance($abstract, $instance) | |
{ | |
$this->getProxy()->instance($abstract, $instance); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function when($concrete) | |
{ | |
$this->getProxy()->when($concrete); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function call($callback, array $parameters = [], $defaultMethod = null) | |
{ | |
$this->getProxy()->call($callback, $parameters, $defaultMethod); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function resolved($abstract) | |
{ | |
$this->getProxy()->resolved($abstract); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function resolving($abstract, Closure $callback = null) | |
{ | |
$this->getProxy()->resolving($abstract, $callback); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Exception | |
*/ | |
public function afterResolving($abstract, Closure $callback = null) | |
{ | |
$this->getProxy()->afterResolving($abstract, $callback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment