Passed
Push — main ( bc0d21...d7ecf8 )
by Thierry
20:15 queued 10:22
created

setupDi()   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 2
nop 2
dl 0
loc 46
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace Jaxon\Upload;
4
5
use Jaxon\App\Config\ConfigListenerInterface;
6
use Jaxon\App\Config\ConfigManager;
7
use Jaxon\Config\Config;
8
use Jaxon\App\I18n\Translator;
9
use Jaxon\Request\Upload\UploadHandlerInterface;
10
use Jaxon\Upload\Manager\FileNameInterface;
11
use Jaxon\Upload\Manager\FileStorage;
12
use Jaxon\Upload\Manager\UploadManager;
13
use Jaxon\Upload\Manager\Validator;
14
15
use function Jaxon\jaxon;
16
use function bin2hex;
17
use function php_sapi_name;
18
use function random_bytes;
19
use function realpath;
20
21
/**
22
 * @return void
23
 */
24
function registerUpload()
25
{
26
    $jaxon = jaxon();
27
    $di = $jaxon->di();
28
    if($di->h(UploadHandler::class))
29
    {
30
        return;
31
    }
32
33
    // Upload file and dir name generator
34
    $di->set(FileNameInterface::class, function() {
35
        return new class implements FileNameInterface
36
        {
37
            public function random(int $nLength): string
38
            {
39
                return bin2hex(random_bytes((int)($nLength / 2)));
40
            }
41
        };
42
    });
43
    // Upload validator
44
    $di->set(Validator::class, function($c) {
45
        return new Validator($c->g(ConfigManager::class), $c->g(Translator::class));
46
    });
47
    // File storage
48
    $di->set(FileStorage::class, function($c) {
49
        $xFileStorage = new FileStorage($c->g(ConfigManager::class), $c->g(Translator::class));
50
        $xFileStorage->registerAdapters();
51
        return $xFileStorage;
52
    });
53
    // File upload manager
54
    $di->set(UploadManager::class, function($c) {
55
        // Translation directory
56
        $sTranslationDir = realpath(__DIR__ . '/../../translations');
57
        // Load the upload translations
58
        $xTranslator = $c->g(Translator::class);
59
        $xTranslator->loadTranslations("$sTranslationDir/en/upload.php", 'en');
60
        $xTranslator->loadTranslations("$sTranslationDir/fr/upload.php", 'fr');
61
        $xTranslator->loadTranslations("$sTranslationDir/es/upload.php", 'es');
62
63
        return new UploadManager($c->g(FileStorage::class), $c->g(FileNameInterface::class),
64
            $c->g(ConfigManager::class), $c->g(Validator::class), $xTranslator);
65
    });
66
    // File upload plugin
67
    $di->set(UploadHandler::class, function($c) {
68
        return new UploadHandler($c->g(FileStorage::class), $c->g(UploadManager::class));
69
    });
70
    // Set alias on the interface
71
    $di->alias(UploadHandlerInterface::class, UploadHandler::class);
72
73
    // Set a callback to process uploaded files in the incoming requests.
74
    $jaxon->callback()->before(function() use($jaxon, $di) {
75
        if(!$jaxon->getOption('core.upload.enabled'))
76
        {
77
            return;
78
        }
79
        /** @var UploadHandler */
80
        $xUploadHandler = $di->g(UploadHandler::class);
81
        // The HTTP request
82
        $xRequest = $di->getRequest();
83
        if($xUploadHandler->canProcessRequest($xRequest))
84
        {
85
            $xUploadHandler->processRequest($xRequest);
86
        }
87
    });
88
}
89
90
/**
91
 * Register the values into the container
92
 *
93
 * @return void
94
 */
95
function _register()
96
{
97
    $jaxon = jaxon();
98
    $di = $jaxon->di();
99
    $sEventListenerKey = UploadHandler::class . '\\ConfigListener';
100
    if($di->h($sEventListenerKey))
101
    {
102
        return;
103
    }
104
105
    // The upload package is installed, the upload manager must be registered,
106
    // but only when the feature is activated in the config.
107
    $di->set($sEventListenerKey, function() {
108
        return new class implements ConfigListenerInterface
109
        {
110
            public function onChange(Config $xConfig, string $sName)
111
            {
112
                $sConfigKey = 'core.upload.enabled';
113
                if(($sName === $sConfigKey || $sName === '') && $xConfig->getOption($sConfigKey))
114
                {
115
                    registerUpload();
116
                }
117
            }
118
        };
119
    });
120
121
    // Listener for app config changes.
122
    $jaxon->config()->addLibEventListener($sEventListenerKey);
123
}
124
125
function register()
126
{
127
    // Do nothing if running in cli.
128
    if(php_sapi_name() !== 'cli')
129
    {
130
        _register();
131
    };
132
}
133
134
register();
135