-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultController.php
More file actions
70 lines (65 loc) · 2.63 KB
/
DefaultController.php
File metadata and controls
70 lines (65 loc) · 2.63 KB
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
<?php
namespace MaplePHP\Unitary\Console\Controllers;
use MaplePHP\Emitron\Contracts\ConfigPropsInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use MaplePHP\Emitron\Contracts\DispatchConfigInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use MaplePHP\Prompts\Command;
use MaplePHP\Validate\Validator;
abstract class DefaultController
{
protected readonly ServerRequestInterface|RequestInterface $request;
protected readonly ContainerInterface $container;
protected Command $command;
protected DispatchConfigInterface $configs;
protected array $args;
protected ?ConfigPropsInterface $props = null;
protected string|bool $path;
/**
* Set some data type safe object that comes from container and the dispatcher
*
* @param ContainerInterface $container
* @param ResponseInterface $response
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ErrorException
*/
public function __construct(ContainerInterface $container, ResponseInterface $response)
{
$this->container = $container;
$this->args = $this->container->get("args");
$this->props = $this->container->get("props");
$this->command = $this->container->get("command");
$this->request = $this->container->get("request");
$this->configs = $this->container->get("configuration");
$defaultPath = $this->container->get("request")->getUri()->getDir();
$defaultPath = ($this->configs->getProps()->path !== null) ? $this->configs->getProps()->path : $defaultPath;
$this->path = realpath($this->args['path'] ?? $defaultPath);
$this->forceShowHelp($response);
}
/**
* This is a temporary solution that will show help if a user
* writes a wrong argv param in CLI
*
* @param ResponseInterface $response
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \ErrorException
*/
protected function forceShowHelp(ResponseInterface $response): void
{
if (!Validator::value($response->getStatusCode())->isHttpSuccess()) {
$props = $this->configs->getProps();
$help = ($props->helpController !== null) ?
$props->helpController : "\MaplePHP\Unitary\Console\Controllers\HelpController";
$help = new $help($this->container, $response->withStatus(200));
$help->index();
exit(1);
}
}
}