CallableDirPlugin   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 93
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A checkDirectory() 0 8 2
A __construct() 0 3 1
A register() 0 9 2
A getCallable() 0 3 1
A checkOptions() 0 27 5
A getName() 0 3 1
1
<?php
2
3
/**
4
 * CallableDirPlugin.php - Jaxon callable dir plugin
5
 *
6
 * This class registers directories containing user defined callable classes,
7
 * and generates client side javascript code.
8
 *
9
 * @package jaxon-core
10
 * @author Jared White
11
 * @author J. Max Wilson
12
 * @author Joseph Woolley
13
 * @author Steffen Konerow
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
16
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
17
 * @copyright 2016 Thierry Feuzeu <[email protected]>
18
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
19
 * @link https://github.com/jaxon-php/jaxon-core
20
 */
21
22
namespace Jaxon\Plugin\Request\CallableClass;
23
24
use Jaxon\Jaxon;
25
use Jaxon\App\I18n\Translator;
26
use Jaxon\Di\ClassContainer;
27
use Jaxon\Exception\SetupException;
28
use Jaxon\Plugin\CallableRegistryInterface;
29
use Jaxon\Plugin\PluginInterface;
30
31
use function is_array;
32
use function is_dir;
33
use function is_string;
34
use function realpath;
35
use function rtrim;
36
use function str_replace;
37
use function trim;
38
39
class CallableDirPlugin implements PluginInterface, CallableRegistryInterface
40
{
41
    /**
42
     * The class constructor
43
     *
44
     * @param ClassContainer $cls
45
     * @param CallableRegistry $xRegistry
46
     * @param Translator $xTranslator
47
     */
48
    public function __construct(protected ClassContainer $cls,
49
        protected CallableRegistry $xRegistry, protected Translator $xTranslator)
50
    {}
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function getName(): string
56
    {
57
        return Jaxon::CALLABLE_DIR;
58
    }
59
60
    /**
61
     * Check the directory
62
     *
63
     * @param string $sDirectory    The path of teh directory being registered
64
     *
65
     * @return string
66
     * @throws SetupException
67
     */
68
    private function checkDirectory(string $sDirectory): string
69
    {
70
        $sDirectory = rtrim(trim($sDirectory), '/\\');
71
        if(!is_dir($sDirectory))
72
        {
73
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
74
        }
75
        return realpath($sDirectory);
76
    }
77
78
    /**
79
     * @inheritDoc
80
     * @throws SetupException
81
     */
82
    public function checkOptions(string $sCallable, $xOptions): array
83
    {
84
        if(is_string($xOptions))
85
        {
86
            $xOptions = ['namespace' => $xOptions];
87
        }
88
        if(!is_array($xOptions))
89
        {
90
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
91
        }
92
        // Check the directory
93
        $xOptions['directory'] = $this->checkDirectory($sCallable);
94
        // Check the namespace
95
        $sNamespace = $xOptions['namespace'] ?? '';
96
        if(!($xOptions['namespace'] = trim($sNamespace, ' \\')))
97
        {
98
            $xOptions['namespace'] = '';
99
        }
100
101
        // Change the keys in $xOptions to have "\" as separator
102
        $_aOptions = [];
103
        foreach($xOptions as $sName => $aOption)
104
        {
105
            $sName = trim(str_replace('.', '\\', $sName), ' \\');
106
            $_aOptions[$sName] = $aOption;
107
        }
108
        return $_aOptions;
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function register(string $sType, string $sCallable, array $aOptions): bool
115
    {
116
        if(($aOptions['namespace']))
117
        {
118
            $this->xRegistry->registerNamespace($aOptions['namespace'], $aOptions);
119
            return true;
120
        }
121
        $this->xRegistry->registerDirectory($aOptions['directory'], $aOptions);
122
        return true;
123
    }
124
125
    /**
126
     * @inheritDoc
127
     * @throws SetupException
128
     */
129
    public function getCallable(string $sCallable)
130
    {
131
        return $this->cls->makeCallableObject($sCallable);
132
    }
133
}
134