Test Failed
Pull Request — master (#37)
by Divine Niiquaye
03:08
created

PhpExtension   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 21
c 1
b 0
f 0
dl 0
loc 54
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 0 3 1
C boot() 0 20 12
A register() 0 3 1
A setConfig() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2021 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\DI\Extensions;
19
20
use Rade\DI\{AbstractContainer, Container};
21
22
/**
23
 * PHP directives definition.
24
 *
25
 * @author Divine Niiquaye Ibok <[email protected]>
26
 */
27
class PhpExtension implements AliasedInterface, BootExtensionInterface, ExtensionInterface
28
{
29
    /** @var array<string,string> */
30
    private array $config = [];
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getAlias(): string
36
    {
37
        return 'php';
38
    }
39
40
    /**
41
     * Set a config supported by this extension.
42
     *
43
     * @param mixed $value
44
     */
45
    public function setConfig(string $key, $value): void
46
    {
47
        $this->config[$key] = $value;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function register(AbstractContainer $container, array $configs): void
54
    {
55
        $this->config = $configs;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function boot(AbstractContainer $container): void
62
    {
63
        if ($container instanceof Container) {
64
            foreach ($this->config as $name => $value) {
65
                if (null === $value) {
66
                    continue;
67
                }
68
69
                if ('include_path' === $name) {
70
                    \set_include_path(\str_replace(';', \PATH_SEPARATOR, $value));
71
                } elseif ('ignore_user_abort' === $name) {
72
                    \ignore_user_abort(null === $value ? $value : (bool) $value);
73
                } elseif ('max_execution_time' === $name) {
74
                    \set_time_limit((int) $value);
75
                } elseif ('date.timezone' === $name) {
76
                    \date_default_timezone_set($value);
77
                } elseif (\function_exists('ini_set')) {
78
                    \ini_set($name, false === $value ? '0' : (string) $value);
79
                } elseif (\ini_get($name) !== (string) $value) {
80
                    throw new \InvalidArgumentException('Required function ini_set() is disabled.');
81
                }
82
            }
83
        }
84
    }
85
}
86