Issues (718)

src/Charcoal/Admin/Config.php (1 issue)

Severity
1
<?php
2
3
namespace Charcoal\Admin;
4
5
use InvalidArgumentException;
6
7
// From 'charcoal-core'
8
use Charcoal\Config\AbstractConfig;
9
10
// From 'charcoal-app'
11
use Charcoal\App\Handler\HandlerConfig;
12
use Charcoal\App\Route\RouteConfig;
13
14
/**
15
 * Admin Config.
16
 */
17
class Config extends AbstractConfig
18
{
19
    const DEFAULT_BASE_PATH = 'admin';
20
21
    /**
22
     * The base path for the admin module's route group.
23
     *
24
     * @var string $basePath
25
     */
26
    private $basePath = self::DEFAULT_BASE_PATH;
27
28
    /**
29
     * @var array
30
     */
31
    public $routes = [];
32
33
    /**
34
     * @var array
35
     */
36
    private $handlers = [];
37
38
    /**
39
     * @var array
40
     */
41
    public $acl = [];
42
43
    /**
44
     * The default data is defined in a JSON file.
45
     *
46
     * @return array
47
     */
48
    public function defaults()
49
    {
50
        $baseDir = rtrim(realpath(__DIR__.'/../../../'), '/');
51
        $confDir = $baseDir.'/config';
52
53
        return $this->loadFile($confDir.'/admin.config.default.json');
54
    }
55
56
    /**
57
     * Set the admin module's route group.
58
     *
59
     * @param  string $path The admin module base path.
60
     * @throws InvalidArgumentException If the route group is invalid.
61
     * @return self
62
     */
63
    public function setBasePath($path)
64
    {
65
        if (!is_string($path)) {
0 ignored issues
show
The condition is_string($path) is always true.
Loading history...
66
            throw new InvalidArgumentException(
67
                'Path must be a string'
68
            );
69
        }
70
71
        // Can not be empty
72
        if ($path == '') {
73
            throw new InvalidArgumentException(
74
                'Path can not be empty'
75
            );
76
        }
77
78
        $this->basePath = $path;
79
        return $this;
80
    }
81
82
    /**
83
     * Retrieve the admin module's route group.
84
     *
85
     * @return string
86
     */
87
    public function basePath()
88
    {
89
        return $this->basePath;
90
    }
91
92
    /**
93
     * Parse the admin module's route configuration.
94
     *
95
     * @see    \Charcoal\App\AppConfig::setRoutes() For a similar implementation.
96
     * @param  array $routes The route configuration structure to set.
97
     * @return self
98
     */
99
    public function setRoutes(array $routes)
100
    {
101
        $toIterate = RouteConfig::defaultRouteTypes();
102
        foreach ($routes as $key => $val) {
103
            if (in_array($key, $toIterate) && isset($this->routes[$key])) {
104
                $this->routes[$key] = array_merge($this->routes[$key], $val);
105
            } else {
106
                $this->routes[$key] = $val;
107
            }
108
        }
109
110
        return $this;
111
    }
112
113
    /**
114
     * Define custom response and error handlers.
115
     *
116
     * Charcoal overrides four of Slim's standard handlers:
117
     *
118
     * - "notFoundHandler"
119
     * - "notAllowedHandler"
120
     * - "errorHandler"
121
     * - "phpErrorHandler"
122
     *
123
     * @param  array $handlers The handlers configuration structure to set.
124
     * @return self
125
     */
126
    public function setHandlers(array $handlers)
127
    {
128
        $this->handlers = array_fill_keys(HandlerConfig::defaultHandlerTypes(), []);
129
        $this->handlers['defaults'] = [];
130
131
        foreach ($handlers as $handler => $data) {
132
            $this->handlers[$handler] = array_replace(
133
                $this->handlers[$handler],
134
                $data
135
            );
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function handlers()
145
    {
146
        return $this->handlers;
147
    }
148
}
149