1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Folk; |
4
|
|
|
|
5
|
|
|
use Fol; |
6
|
|
|
use Folk\Entities\EntityInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Zend\Diactoros\Response; |
10
|
|
|
use Relay\RelayBuilder; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Main manager. |
14
|
|
|
*/ |
15
|
|
|
class Admin extends Fol |
16
|
|
|
{ |
17
|
|
|
private $entities = []; |
18
|
|
|
|
19
|
|
|
public $title = 'Folk'; |
20
|
|
|
public $description = 'Universal CMS'; |
21
|
|
|
|
22
|
|
|
public function __construct($url) |
23
|
|
|
{ |
24
|
|
|
$this->setUrl($url); |
25
|
|
|
|
26
|
|
|
$this->register(new Providers\Builder()); |
27
|
|
|
$this->register(new Providers\Middlewares()); |
28
|
|
|
$this->register(new Providers\Router()); |
29
|
|
|
$this->register(new Providers\Templates()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return ResponseInterface |
34
|
|
|
*/ |
35
|
|
|
public function __invoke(ServerRequestInterface $request) |
36
|
|
|
{ |
37
|
|
|
$dispatcher = (new RelayBuilder())->newInstance($this['middlewares']); |
38
|
|
|
|
39
|
|
|
return $dispatcher($request, new Response()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getRoute($name, array $data = array(), array $query = null) |
43
|
|
|
{ |
44
|
|
|
return $this->getUrl($this['router']->getGenerator()->generate($name, $data)).($query ? '?'.http_build_query($query) : ''); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set the admin entities. |
49
|
|
|
* |
50
|
|
|
* @param array $entities |
51
|
|
|
*/ |
52
|
|
|
public function setEntities(array $entities) |
53
|
|
|
{ |
54
|
|
|
foreach ($entities as $name => $entity) { |
55
|
|
|
if (is_int($name)) { |
56
|
|
|
$name = strtolower(substr(strrchr($entity, '\\'), 1)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->addEntity(new $entity($name, $this)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add a new entity. |
65
|
|
|
* |
66
|
|
|
* @param EntityInterface $entity |
67
|
|
|
*/ |
68
|
|
|
public function addEntity(EntityInterface $entity) |
69
|
|
|
{ |
70
|
|
|
if (empty($entity->title)) { |
|
|
|
|
71
|
|
|
$entity->title = ucfirst($entity->getName()); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->entities[$entity->getName()] = $entity; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return an entity. |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
* |
82
|
|
|
* @return EntityInterface|null |
83
|
|
|
*/ |
84
|
|
|
public function getEntity($name) |
85
|
|
|
{ |
86
|
|
|
return isset($this->entities[$name]) ? $this->entities[$name] : null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return all entities. |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getAllEntities() |
95
|
|
|
{ |
96
|
|
|
return $this->entities; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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: