ServiceAwareTrait::getServiceLocator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) Florian Krämer
4
 *
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright     Copyright (c) Florian Krämer
10
 * @link          https://github.com/burzum/cakephp-service-layer
11
 * @since         1.0.0
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
declare(strict_types=1);
15
16
namespace Burzum\CakeServiceLayer\Service;
17
18
use Cake\Core\ObjectRegistry;
19
20
/**
21
 * Service Aware Trait
22
 *
23
 * CakePHP style locator to load service classes
24
 */
25
trait ServiceAwareTrait
26
{
27
    /**
28
     * Service Locator Registry
29
     *
30
     * @var \Cake\Core\ObjectRegistry
31
     */
32
    protected $serviceLocator;
33
34
    /**
35
     * Default Service Locator Class
36
     *
37
     * @var string
38
     */
39
    protected $defaultServiceLocator = ServiceLocator::class;
40
41
    /**
42
     * Load a service
43
     *
44
     * If the service is in a subfolder, this folder name will be stripped for the property name when assigned.
45
     *
46
     * @param string $service Service Name
47
     * @param array $constructorArgs Constructor Args
48
     * @param bool $assignProperty Assigns the service to a class property of the same name as  the service
49
     * @return object
50
     */
51
    public function loadService($service, array $constructorArgs = [], $assignProperty = true)
52
    {
53
        [, $name] = pluginSplit($service);
54
55
        if (strpos($name, '/') !== false) {
56
            $name = substr($name, strrpos($name, '/') + 1);
57
        }
58
59
        if ($assignProperty && isset($this->{$name})) {
60
            return $this->{$name};
61
        }
62
63
        $serviceInstance = $this->getServiceLocator()->load($service, $constructorArgs);
64
65
        if (!$assignProperty) {
66
            return $serviceInstance;
67
        }
68
69
        $this->{$name} = $serviceInstance;
70
71
        return $serviceInstance;
72
    }
73
74
    /**
75
     * Get the service locator
76
     *
77
     * @return \Cake\Core\ObjectRegistry
78
     */
79
    public function getServiceLocator()
80
    {
81
        if (empty($this->serviceLocator)) {
82
            $class = $this->defaultServiceLocator;
83
            $this->serviceLocator = new $class();
84
        }
85
86
        return $this->serviceLocator;
87
    }
88
89
    /**
90
     * Sets the service locator
91
     *
92
     * @param \Cake\Core\ObjectRegistry $locator Locator
93
     * @return void
94
     */
95
    public function setServiceLocator(ObjectRegistry $locator)
96
    {
97
        $this->serviceLocator = $locator;
98
    }
99
}
100