2. About Us
• Akihito Koriyama
• A Tokyo based freelance PHP architect
• BEAR.Sunday lead, Aura PHP member
• Richard McIntyre
• Manchester / leeds based freelance PHP
developer
• BEAR.Sunday contributor, Lithium member
3. About Us
• Akihito Koriyama
• A Tokyo based freelance PHP architect
• BEAR.Sunday lead, Aura PHP member
• Richard McIntyre
• Manchester / Leeds based freelance PHP
developer
• BEAR.Sunday contributor, Lithium member
4.
5. BEAR.Sunday is an application framework.
But it offers no libraries.
(We have good stuff)
13. DI Benefits
• Dependency Inversion Principle
• Clear separation of object instantiation and object
usage
• Object delivery
14. DIP
• Code should depend on
things that are at the same
or higher level of
abstraction
• High level policy should not
depend on low level details
15. “The principle of dependency inversion is at the
root of many of the benefits claimed for object-
oriented technology.
Its proper application is necessary for the creation
of reusable frameworks”
17. /**
* @Inject
*/
public function __construct(RenderInterface $renderer)
{
...
1.annotate at injection point
class RendererModule extends AbstractModule
{
protected function configure()
{
$this->bind('RenderInterface')
->to('HalRenderer')
->in(Scope::SINGLETON);
}
$injector = Inject::create([new RendererModule]];
2 bind abstraction to concretion
3.create an injector
18. /**
* @Inject
*/
public function __construct(RenderInterface $renderer)
{
...
1.annotate at injection point
class RendererModule extends AbstractModule
{
protected function configure()
{
$this->bind('RenderInterface')
->to('HalRenderer')
->in(Scope::SINGLETON);
}
$injector = Inject::create([new RendererModule]];
2. bind abstraction to concretion
3.create an injector
19. /**
* @Inject
*/
public function __construct(RenderInterface $renderer)
{
...
1.annotate at injection point
class RendererModule extends AbstractModule
{
protected function configure()
{
$this->bind('RenderInterface')
->to('HalRenderer')
->in(Scope::SINGLETON);
}
$injector = Inject::create([new RendererModule]];
2 bind abstraction to concretion
3.create an injector
22. class RendererModule extends AbstractModule
{
protected function configure()
{
$this
->bind('RenderInterface')
->to('HalRenderer')
->in(Scope::SINGLETON);
}
}
Only use concrete classes in compilation
23. Only abstraction in runtime
/**
* @Inject
*/
public function __construct(RenderInterface $renderer)
{
24. DI Best practice
“Your code should deal directly with the Injector
as little as possible. Instead, you want to bootstrap
your application by injecting one root object.”
51. <?php
class SandboxResourcePageIndexRay0000000071f9ab280000000033fb446fAop extends
SandboxResourcePageIndex implements RayAopWeavedInterface
{
private $rayAopIntercept = true;
public $rayAopBind;
public function onGet()
{
// native call
if (!isset($this->rayAopBind[__FUNCTION__])) {
return call_user_func_array('parent::' . __FUNCTION__, func_get_args());
}
// proceed source method from interceptor
if (!$this->rayAopIntercept) {
$this->rayAopIntercept = true;
return call_user_func_array('parent::' . __FUNCTION__, func_get_args());
}
// proceed next interceptor
$this->rayAopIntercept = false;
$interceptors = $this->rayAopBind[__FUNCTION__];
$annotation = isset($this->rayAopBind->annotation[__FUNCTION__]) ? $this->rayAopBind
>annotation[__FUNCTION__] : null;
$invocation = new RayAopReflectiveMethodInvocation(array($this, __FUNCTION__),
func_get_args(), $interceptors, $annotation);
return $invocation->proceed();
}
Under the hood: Method interception sub class is created
in order enable this interception and keep type safety.
53. 2nd framework: Aspect Oriented Framework
• AOP alliance standard
• Layering by context
• Type safe
• Runtime injection
54.
55. Hypermedia framework for object as a service
It allows objects to have RESTful web service benefits
such as client-server, uniform interface, statelessness,
resource expression with mutual connectivity and
layered components.
56. class Author extends ResourceObject
{
public $code = 200;
public $headers = [];
public $body = [];
/**
* @Link(rel="blog", href="app://self/blog/post?author_id={id}")
*/
public function onGet($id)
{
... // change own state
return $this;
}
public function onPost($name)
{
$this->code = 201; // created
return $this;
}
public function onPut($id, $name)
58. $order = $resource
->post
->uri('app://self/order')
->withQuery(['drink' => 'latte'])
->eager
->request();
$payment = [
'credit_card_number' => '123456789',
'expires' => '07/07',
'name' => 'Koriym',
'amount' => '4.00'
];
// Now use a hyperlink to pay
$response = $resource->href('pay', $payment);
echo $response->code; // 201
Hypermedia as the Engine of Application State
59. class Order extends ResourceObject
{
/**
*
* @Link(rel="pay", method="put",
href="app://self/payment{?order_id,card_number,amount}")
*/
public function onPost($drink)
{
// data store here
$this['drink'] = $drink;
$this['order_id'] = $orderId;
// created
$this->code = 201;
return $this;
}
Hypermedia as the Engine of Application State