|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\DI; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Trait to create a set of base services. |
|
7
|
|
|
*/ |
|
8
|
|
|
trait DIServiceSetBaseTrait |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Create a set of basic services useful for a website. |
|
12
|
|
|
* |
|
13
|
|
|
* @return self |
|
14
|
|
|
*/ |
|
15
|
2 |
|
public function createDefaultServices() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->setShared("request", function () { |
|
|
|
|
|
|
18
|
|
|
$request = new \Anax\Request\Request(); |
|
19
|
|
|
$request->init(); |
|
20
|
|
|
return $request; |
|
21
|
2 |
|
}); |
|
22
|
|
|
|
|
23
|
2 |
|
$this->setShared("response", "\Anax\Response\Response"); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
View Code Duplication |
$this->setShared("url", function () { |
|
|
|
|
|
|
26
|
|
|
$url = new \Anax\Url\Url(); |
|
27
|
|
|
$request = $this->get("request"); |
|
|
|
|
|
|
28
|
|
|
$url->setSiteUrl($request->getSiteUrl()); |
|
29
|
|
|
$url->setBaseUrl($request->getBaseUrl()); |
|
30
|
|
|
$url->setStaticSiteUrl($request->getSiteUrl()); |
|
31
|
|
|
$url->setStaticBaseUrl($request->getBaseUrl()); |
|
32
|
|
|
$url->setScriptName($request->getScriptName()); |
|
33
|
|
|
$url->configure("url.php"); |
|
34
|
|
|
$url->setDefaultsFromConfiguration(); |
|
35
|
|
|
return $url; |
|
36
|
2 |
|
}); |
|
37
|
|
|
|
|
38
|
|
|
$this->setShared("router", function () { |
|
|
|
|
|
|
39
|
|
|
$router = new \Anax\Route\Router(); |
|
40
|
|
|
$router->setDI($this); |
|
41
|
|
|
return $router; |
|
42
|
2 |
|
}); |
|
43
|
|
|
|
|
44
|
|
|
$this->setShared("view", function () { |
|
|
|
|
|
|
45
|
|
|
$view = new \Anax\View\ViewContainer(); |
|
46
|
|
|
$view->configure("view.php"); |
|
47
|
|
|
$view->setDI($this); |
|
48
|
|
|
return $view; |
|
49
|
2 |
|
}); |
|
50
|
|
|
|
|
51
|
|
|
$this->setShared("viewRenderFile", function () { |
|
|
|
|
|
|
52
|
|
|
$viewRender = new \Anax\View\ViewRenderFile2(); |
|
53
|
|
|
$viewRender->setDI($this); |
|
54
|
|
|
return $viewRender; |
|
55
|
2 |
|
}); |
|
56
|
|
|
|
|
57
|
2 |
|
$this->setShared("session", function () { |
|
|
|
|
|
|
58
|
|
|
$session = new \Anax\Session\SessionConfigurable(); |
|
59
|
|
|
$session->configure("session.php"); |
|
60
|
|
|
return $session; |
|
61
|
2 |
|
}); |
|
62
|
|
|
|
|
63
|
2 |
|
$this->setShared("textfilter", "\Anax\TextFilter\TextFilter"); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
2 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.