Passed
Push — develop ( b729b4...acdfb2 )
by nguereza
42:48
created

AbstractApplication::addCoreServiceProviders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 22
rs 9.7998
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file AbstractApplication.php
34
 *
35
 *  The Platine Base Application class
36
 *
37
 *  @package    Platine\Framework
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework;
49
50
use Platine\Config\Config;
51
use Platine\Config\FileLoader;
52
use Platine\Config\LoaderInterface;
53
use Platine\Container\ConstructorResolver;
54
use Platine\Container\Container;
55
use Platine\Container\ContainerInterface;
56
use Platine\Container\ResolverInterface;
57
use Platine\Event\Dispatcher;
58
use Platine\Event\DispatcherInterface;
59
use Platine\Framework\Http\Emitter\EmitterInterface;
60
use Platine\Framework\Http\Emitter\ResponseEmitter;
61
use Platine\Http\Handler\CallableResolver;
62
use Platine\Http\Handler\CallableResolverInterface;
63
use Platine\Http\Handler\RequestHandlerInterface;
64
use Platine\Logger\Configuration;
65
use Platine\Logger\Logger;
66
use Platine\Logger\LoggerInterface;
67
68
/**
69
 * class AbstractApplication
70
 * @package Platine\Framework
71
 */
72
abstract class AbstractApplication extends Container
73
{
74
75
    /**
76
     * The application version
77
     */
78
    public const VERSION = '1.0.0-dev';
79
80
    /**
81
     * The base path for this application
82
     * @var string
83
     */
84
    protected string $basePath;
85
86
    /**
87
     * The configuration instance
88
     * @var Config
89
     */
90
    protected Config $config;
91
92
    /**
93
     * The logger instance to use
94
     * @var LoggerInterface
95
     */
96
    protected LoggerInterface $logger;
97
98
    /**
99
     * Create new instance
100
     * @param string|null $basePath
101
     */
102
    public function __construct(?string $basePath = null)
103
    {
104
        parent::__construct();
105
        $this->addCoreServiceProviders();
106
107
        $this->config = $this->get(Config::class);
108
        $this->logger = $this->get(LoggerInterface::class);
109
110
        $this->basePath = $basePath ?? $this->config->get('app.base_path', '/');
111
    }
112
113
    /**
114
     * Execute the application
115
     * @return void
116
     */
117
    abstract public function execute(): void;
118
119
    /**
120
     * Load core service provider
121
     * @return void
122
     */
123
    protected function addCoreServiceProviders(): void
124
    {
125
        $this->bind(LoaderInterface::class, FileLoader::class, [
126
            'path' => __DIR__ . '/../../../packages/app/config'
127
        ]);
128
        $this->share(Config::class);
129
        $this->bind(ContainerInterface::class, $this);
130
        $this->bind(ResolverInterface::class, ConstructorResolver::class);
131
        $this->bind(CallableResolverInterface::class, CallableResolver::class);
132
        $this->bind(RequestHandlerInterface::class, $this);
133
        $this->bind(EmitterInterface::class, function (ContainerInterface $app) {
134
            return new ResponseEmitter(
135
                $app->get(Config::class)->get('app.response_length', null)
136
            );
137
        });
138
139
        $this->bind(LoggerInterface::class, function (ContainerInterface $app) {
140
            return new Logger(
141
                new Configuration($app->get(Config::class)->get('logging', []))
142
            );
143
        });
144
        $this->bind(DispatcherInterface::class, Dispatcher::class);
145
    }
146
}
147