Completed
Push — dev-master ( 86a9d6...915e4b )
by Derek Stephen
05:17
created

Application::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Bone\Mvc;
4
5
use Bone\Server\Environment;
6
use Pimple\Container;
7
use Zend\Diactoros\ServerRequestFactory;
8
use Zend\Diactoros\Response;
9
10
class Application
11
{
12
    /** @var Registry $registry */
13
    private $registry;
14
15
    /** @var Container $registry */
16
    private $treasureChest;
17
18
    /** @var string $configFolder */
19
    private $configFolder = 'config';
20
21
    /** @var string $environment */
22
    private $environment = 'production';
23
24
    /**
25
     *  There be nay feckin wi' constructors on board this ship
26
     *  There be nay copyin' o' th'ship either
27
     *  This ship is a singleton!
28
     */
29
    public function __construct(){}
30
    public function __clone(){}
31
32
33
    /**
34
     *  Ahoy! There nay be boardin without yer configuration
35
     *
36
     * @param array $config
37
     * @return Application
38
     */
39 2
    public static function ahoy(array $config = [])
40
    {
41 2
        static $inst = null;
42 2
        if ($inst === null)
43
        {
44 1
            $inst = new Application();
45 1
            $inst->registry = Registry::ahoy();
46 1
            $inst->treasureChest = new Container();
47 1
            $inst->setConfig($config);
48 1
            $env = getenv('APPLICATION_ENV');
49 1
            if ($env) {
50 1
                $inst->setEnvironment($env);
51
            }
52
        }
53 2
        return $inst;
54
    }
55
56
    /**
57
     * @param array $config
58
     */
59 1
    private function setConfig(array $config)
60
    {
61 1
        foreach($config as $key => $value)
62
        {
63 1
            $this->registry->set($key,$value);
64 1
            $this->treasureChest[$key] = $value;
65
        }
66 1
    }
67
68
    /**
69
     *
70
     * T' the high seas! Garrr!
71
     *
72
     * @return bool
73
     * @throws \Exception
74
     */
75 1
    public function setSail()
76
    {
77 1
        $env = new Environment($_SERVER);
78 1
        if (!count($this->registry->getAll())) {
79
            $config = $env->fetchConfig($this->configFolder, $this->environment);
80
            $this->setConfig($config);
81
        }
82 1
        $request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
83 1
        $response = new Response();
84 1
        $dispatcher = new Dispatcher($request, $response, $env);
85 1
        $dispatcher->fireCannons();
86
87 1
        return true;
88
    }
89
90
    /**
91
     * @return Container
92
     */
93
    public function getContainer(): Container
94
    {
95
        return $this->treasureChest;
96
    }
97
98
    /**
99
     * @param string $configFolder
100
     */
101
    public function setConfigFolder(string $configFolder)
102
    {
103
        $this->configFolder = $configFolder;
104
    }
105
106
    /**
107
     * @param string $environment
108
     */
109 1
    public function setEnvironment(string $environment)
110
    {
111 1
        $this->environment = $environment;
112
    }
113
}