/
Service.php
139 lines (122 loc) · 3.29 KB
/
Service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace GeometriaLab\Permissions\Assertion;
use Zend\Stdlib\Glob as ZendGlob,
Zend\ServiceManager\FactoryInterface as ZendFactoryInterface,
Zend\ServiceManager\ServiceLocatorInterface as ZendServiceLocatorInterface;
class Service implements ZendFactoryInterface
{
/**
* @var Assertion
*/
private $assertion;
/**
* @var array
*/
private $config = array();
/**
* @var ZendServiceLocatorInterface
*/
private $serviceLocator;
/**
* Create service
*
* @param ZendServiceLocatorInterface $serviceLocator
* @return Assertion
*/
public function createService(ZendServiceLocatorInterface $serviceLocator)
{
$this->setServiceLocator($serviceLocator);
$config = $serviceLocator->get('Configuration');
if (isset($config['assertion'])) {
$this->setConfig($config['assertion']);
}
$this->addResources();
return $this->getAssertion();
}
/**
* Set Service Locator
*
* @param ZendServiceLocatorInterface $serviceLocator
*/
public function setServiceLocator(ZendServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}
/**
* Get Service Locator
*
* @return ZendServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->serviceLocator;
}
/**
* Set config
*
* @param array $config
* @return Service
*/
public function setConfig($config)
{
$this->config = $config;
return $this;
}
/**
* Get Assertion object
*
* @return Assertion
*/
public function getAssertion()
{
if ($this->assertion === null) {
$this->assertion = new Assertion();
}
return $this->assertion;
}
/**
* Add all resources
*
* @return Service
*/
private function addResources()
{
$namespace = $this->getNamespace();
$pathPattern = $this->getResourcesPath() . '*';
foreach (ZendGlob::glob($pathPattern, ZendGlob::GLOB_BRACE) as $file) {
/* @var \GeometriaLab\Permissions\Assertion\Resource\ResourceInterface $resource */
$resourceName = ucfirst(pathinfo($file, PATHINFO_FILENAME));
$resourceClassName = $namespace . '\\' . $resourceName;
$this->getAssertion()->addResource(
new $resourceClassName($resourceName)
);
}
return $this;
}
/**
* Get Permissions' namespace
*
* @return string
* @throws \InvalidArgumentException
*/
private function getNamespace()
{
if (empty($this->config['__NAMESPACE__'])) {
throw new \InvalidArgumentException('Need not empty "assertion.__NAMESPACE__" param in config');
}
return $this->config['__NAMESPACE__'];
}
/**
* Get path to the resources
*
* @return string
* @throws \InvalidArgumentException
*/
private function getResourcesPath()
{
if (empty($this->config['base_dir'])) {
throw new \InvalidArgumentException('Need not empty "assertion.base_dir" param in config');
}
return rtrim($this->config['base_dir'], '/') . '/';
}
}