1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Folk; |
4
|
|
|
|
5
|
|
|
use Fol\{App, NotFoundException}; |
6
|
|
|
use Folk\Entities\EntityInterface; |
7
|
|
|
use Psr\Http\Message\{ServerRequestInterface, ResponseInterface, UriInterface}; |
8
|
|
|
use Interop\Http\ServerMiddleware\MiddlewareInterface; |
9
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface; |
10
|
|
|
use Zend\Diactoros\Response; |
11
|
|
|
use Relay\RelayBuilder; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Main manager. |
15
|
|
|
*/ |
16
|
|
|
class Admin extends App implements MiddlewareInterface |
17
|
|
|
{ |
18
|
|
|
private $entities = []; |
19
|
|
|
|
20
|
|
|
public $title = 'Folk'; |
21
|
|
|
public $description = 'Universal CMS'; |
22
|
|
|
|
23
|
|
|
public function __construct(UriInterface $uri) |
24
|
|
|
{ |
25
|
|
|
parent::__construct(__DIR__, $uri); |
26
|
|
|
|
27
|
|
|
$this->addServiceProvider(new Providers\Formats()); |
28
|
|
|
$this->addServiceProvider(new Providers\Middleware()); |
29
|
|
|
$this->addServiceProvider(new Providers\Router()); |
30
|
|
|
$this->addServiceProvider(new Providers\Templates()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Use the app as a middleware component. |
35
|
|
|
* |
36
|
|
|
* @param ServerRequestInterface $request |
37
|
|
|
* @param DelegateInterface $delegate |
38
|
|
|
* |
39
|
|
|
* @return ResponseInterface |
40
|
|
|
*/ |
41
|
|
|
public function process(ServerRequestInterface $request, DelegateInterface $delegate) |
42
|
|
|
{ |
43
|
|
|
return $this->__invoke($request); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return ResponseInterface |
48
|
|
|
*/ |
49
|
|
|
public function __invoke(ServerRequestInterface $request): ResponseInterface |
50
|
|
|
{ |
51
|
|
|
$dispatcher = $this->get('middleware'); |
52
|
|
|
|
53
|
|
|
return $dispatcher->dispatch($request); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getRoute(string $name, array $data = [], array $query = null): string |
57
|
|
|
{ |
58
|
|
|
return $this->getUri($this->get('router')->getGenerator()->generate($name, $data)).($query ? '?'.http_build_query($query) : ''); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set the admin entities. |
63
|
|
|
* |
64
|
|
|
* @param array $entities |
65
|
|
|
*/ |
66
|
|
|
public function setEntities(array $entities) |
67
|
|
|
{ |
68
|
|
|
foreach ($entities as $name => $entity) { |
69
|
|
|
if (is_int($name)) { |
70
|
|
|
$name = substr(strrchr($entity, '\\'), 1); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->addEntity(new $entity(strtolower($name), $this)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add a new entity. |
79
|
|
|
* |
80
|
|
|
* @param EntityInterface $entity |
81
|
|
|
*/ |
82
|
|
|
public function addEntity(EntityInterface $entity) |
83
|
|
|
{ |
84
|
|
|
$name = $entity->getName(); |
85
|
|
|
|
86
|
|
|
if (empty($entity->title)) { |
|
|
|
|
87
|
|
|
$entity->title = ucfirst($name); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->entities[$name] = $entity; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return whether an entity exists. |
95
|
|
|
* |
96
|
|
|
* @param string $name |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function hasEntity(string $name): bool |
101
|
|
|
{ |
102
|
|
|
return isset($this->entities[$name]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return an entity. |
107
|
|
|
* |
108
|
|
|
* @param string $name |
109
|
|
|
* |
110
|
|
|
* @throw NotFoundException |
111
|
|
|
* |
112
|
|
|
* @return EntityInterface |
113
|
|
|
*/ |
114
|
|
|
public function getEntity(string $name): EntityInterface |
115
|
|
|
{ |
116
|
|
|
if ($this->hasEntity($name)) { |
117
|
|
|
return $this->entities[$name]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
throw new NotFoundException(sprintf('Entity %s not found', $name)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return all entities. |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getAllEntities(): array |
129
|
|
|
{ |
130
|
|
|
return $this->entities; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: