Skip to content

Instantly share code, notes, and snippets.

@devosc
Last active October 29, 2015 22:36
Show Gist options
  • Save devosc/3f6a235a6f9dfcf6f451 to your computer and use it in GitHub Desktop.
Save devosc/3f6a235a6f9dfcf6f451 to your computer and use it in GitHub Desktop.
Module Service Access
<?php
/**
*
*/
use Mvc5\Service\Config\Service\Service;
return [
//'blog:create' => new Service('Blog\Create\Create'),
'blog:create' => new Service('Blog2-Create'),
] + include __DIR__ . '/../vendor/mvc5/framework/config/alias.php';
public function create($config, array $args = [], callable $callback = null)
{
if (!$config) {
return $config;
}
if (is_string($config)) {
$name = explode(Args::SERVICE_SEPARATOR, $config);
$config = array_shift($name);
return array_reduce(
$name,
function($service, $name) {
return $this->resolve($service[$name]);
},
$this->create($this->configured($config), $args) ?:
($callback && !class_exists($config) ? $callback($config) : $this->make($config, $args))
);
}
if (is_array($config)) {
return $this->create(array_shift($config), $config, $callback);
}
if ($config instanceof Closure) {
return $this->invoke($config, $args, $callback);
}
return $this->resolve($config, $args);
}
protected function dependency($name)
{
$name = explode(Args::SERVICE_SEPARATOR, $name);
$service = array_shift($name);
return array_reduce(
$name,
function($service, $name) {
return $this->resolve($service[$name]);
},
$this->get($service)
);
}
if ($config instanceof ServiceDependency) {
//return $this->get($config->name());
return $this->dependency($config->name());
}
return [
'name' => 'home', //for the url plugin in view templates
'route' => '/',
'controller' => 'Home\Controller', //callable
'children' => [
'blog' => [
'route' => 'blog',
'controller' => '@Blog-Controller.test', //specific method
'children' => [
'remove' => [
'route' => '/remove',
'controller' => '@blog:remove' //call event
],
'create' => [
'route' => '/:author[/:category]',
'defaults' => [
'author' => 'owner',
'category' => 'web'
],
'wildcard' => false,
'controller' => '@blog:create', //call event
//'controller' => function($request) { //named args
//var_dump($request->getPathInfo());
//},
'constraints' => [
'author' => '[a-zA-Z0-9_-]*',
'category' => '[a-zA-Z0-9_-]*'
]
]
],
]
]
];
use Mvc5\Application\App;
use Mvc5\Config\Config;
use Mvc5\Service\Config\Factory\Factory;
use Mvc5\Service\Config\Hydrator\Hydrator;
use Mvc5\Service\Config\ServiceManagerLink\ServiceManagerLink;
use Mvc5\Service\Config\Service\Service;
use Mvc5\Service\Config\ServiceProvider\ServiceProvider;
use Mvc5\Service\Config\Param\Param;
use Mvc5\View\Manager\Manager as ViewManager;
use Mvc5\View\Model\Model;
use Mvc5\View\Model\ViewModel;
use Service\Config\Manager\Manager as ServiceManager;
use Service\Resolver\Manager\Resolver as ManagerResolver;
return [
//'Blog' => Blog\Controller::class,
//'Blog' => new Service(Blog\Controller::class, ['template' => new Param('templates.blog')]),
'Blog2' => new Config([
'Create' => new Service(Blog\Create\Create::class),
]),
'Blog' => new App([
'alias' => [],
'events' => [],
'services' => [
'Controller' => new Service(Blog\Controller::class, ['template' => new Param('templates.blog')]),
'Service\Container' => []
],
'templates' => [
'blog' => __DIR__ . '/../view/blog/index.phtml',
]
]),
/*'Home\Controller' => new Service(
Home\Controller::class,
[new Service(Home\Model::class, ['home'])],
['setModel' => new Service('Home\Model', ['home'])]
),*/
//'Home\Model' => new Service(Home\Model::class, ['home']),
//'Home\Controller' => new Factory(Home\Factory::class),
'Request' => new Request\HttpRequest($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER),
'Response' => Response\HttpResponse::class,
'Response\Response' => 'Response',
'Service\Resolver\Manager' => new ServiceProvider(ManagerResolver::class),
ViewModel::class => Model::class,
//'View\Manager' => new ServiceManager(ViewManager::class)
] + include __DIR__ . '/../vendor/mvc5/framework/config/service.php';
<?php
/**
*
*/
return [
//'blog' => __DIR__ . '/../view/blog/index.phtml',
'blog:create' => __DIR__ . '/../view/blog/create.phtml',
'error/exception' => __DIR__ . '/../vendor/mvc5/framework/view/exception.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'home' => __DIR__ . '/../view/home/index.phtml',
'layout' => __DIR__ . '/../view/layout/layout.phtml',
];
use Mvc5\Config\Config;
public function test_create_string_configured()
{
$mock = $this->getCleanMockForTrait(ManageService::class, ['create']);
$mock->expects($this->once())
->method('configured')
->willReturn(new Config(['bar' => 'bat']));
$mock->expects($this->any())
->method('resolve')
->willReturn(new Config(['bar' => 'baz']));
$this->assertEquals(new Config(['bar' => 'baz']), $mock->create('foo-bar'));
}
public function testDependency($name)
{
return $this->dependency($name);
}
public function test_dependency()
{
$mock = $this->getCleanAbstractMock(Resolver::class, ['dependency', 'testDependency']);
$mock->expects($this->once())
->method('get')
->willReturn(new Config(['bar' => 'bat']));
$mock->expects($this->once())
->method('resolve')
->willReturn(new Config(['bar' => 'baz']));
$this->assertEquals(new Config(['bar' => 'baz']), $mock->testDependency('foo-bar'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment