Last active
December 1, 2015 19:52
-
-
Save jeremykendall/3218207a8da5bc867435 to your computer and use it in GitHub Desktop.
Slim/Pimple bridge concept
This file contains hidden or 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 | |
// See http://pimple.sensiolabs.org/#extending-a-container | |
final class Bridge implements ServiceProviderInterface | |
{ | |
public function __construct(PimpleContainer $container) | |
{ | |
$this->container = $container; | |
} | |
public function register(PimpleContainer $pimple) | |
{ | |
foreach ($this->container->keys() as $key) { | |
$pimple[$key] = $this->container->raw($key); | |
} | |
} | |
} |
This file contains hidden or 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 | |
final class BridgeContainer extends PimpleContainer implements ContainerInterface | |
{ | |
/******************************************************************************** | |
* Methods to satisfy Interop\Container\ContainerInterface | |
*******************************************************************************/ | |
/** | |
* Finds an entry of the container by its identifier and returns it. | |
* | |
* @param string $id Identifier of the entry to look for. | |
* | |
* @throws ContainerValueNotFoundException No entry was found for this identifier. | |
* @throws ContainerException Error while retrieving the entry. | |
* | |
* @return mixed Entry. | |
*/ | |
public function get($id) | |
{ | |
if (!$this->offsetExists($id)) { | |
throw new ContainerValueNotFoundException(sprintf('Identifier "%s" is not defined.', $id)); | |
} | |
return $this->offsetGet($id); | |
} | |
/** | |
* Returns true if the container can return an entry for the given identifier. | |
* Returns false otherwise. | |
* | |
* @param string $id Identifier of the entry to look for. | |
* | |
* @return bool | |
*/ | |
public function has($id) | |
{ | |
return $this->offsetExists($id); | |
} | |
} |
This file contains hidden or 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 | |
namespace Bridge\Test; | |
use Bridge\Bridge; | |
use Bridge\BridgeContainer; | |
use Slim\Container as SlimContainer; | |
use Pimple\Container as PimpleContainer; | |
class BridgeTest extends \PHPUnit_Framework_TestCase | |
{ | |
public function testProvider() | |
{ | |
$pimpleContainer = new PimpleContainer(); | |
$pimpleContainer['test'] = function ($c) { | |
return 'TEST'; | |
}; | |
$bridge = new Bridge($pimpleContainer); | |
$slimContainer = new SlimContainer(); | |
$slimContainer->register($bridge); | |
$this->assertEquals('TEST', $slimContainer->get('test')); | |
} | |
public function testCustomContainer() | |
{ | |
$slimContainer = new SlimContainer(); | |
// Honors Pimple\ServiceProviderInterface | |
$bridge = new Bridge($slimContainer); | |
$myContainer = new BridgeContainer(); | |
$myContainer['test'] = function ($c) { | |
return 'TEST'; | |
}; | |
$myContainer->register($bridge); | |
$this->assertEquals($slimContainer->get('errorHandler'), $myContainer->get('errorHandler')); | |
$this->assertInstanceOf('Slim\Handlers\Error', $myContainer->get('errorHandler')); | |
$this->assertEquals('TEST', $myContainer->get('test')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment