Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Container
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 set
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 has
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 remove
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Service;
15
16/**
17 * Service container class
18 *
19 * @category   Pop
20 * @package    Pop\Service
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.2.0
25 */
26class Container
27{
28
29    /**
30     * Array service locators
31     * @var array
32     */
33    private static array $locators = ['default' => null];
34
35    /**
36     * Set a service locator
37     *
38     * @param  string  $name
39     * @param  Locator $locator
40     * @return void
41     */
42    public static function set(string $name, Locator $locator): void
43    {
44        self::$locators[$name] = $locator;
45    }
46
47    /**
48     * Determine if a service locator has been set
49     *
50     * @param  string $name
51     * @return bool
52     */
53    public static function has(string $name): bool
54    {
55        return (!empty(self::$locators[$name]) && (self::$locators[$name] instanceof Locator));
56    }
57
58    /**
59     * Get a service locator
60     *
61     * @param  string $name
62     * @throws Exception
63     * @return Locator
64     */
65    public static function get(string $name = 'default'): Locator
66    {
67        if (empty(self::$locators[$name])) {
68            throw new Exception("Error: The service locator '" . $name . "' has not been added");
69        }
70        return self::$locators[$name];
71    }
72
73    /**
74     * Remove a service locator
75     *
76     * @param  string $name
77     * @return void
78     */
79    public static function remove(string $name): void
80    {
81        if (isset(self::$locators[$name])) {
82            unset(self::$locators[$name]);
83        }
84    }
85
86}