Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Manager
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
8 / 8
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 registerModules
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 register
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRegistered
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasModule
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unregister
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __set
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Pop PHP Framework (https://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@noladev.com>
7 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
8 * @license    https://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Module;
15
16use InvalidArgumentException;
17use Pop\AbstractManager;
18
19/**
20 * Module manager class
21 *
22 * @category   Pop
23 * @package    Pop\Module
24 * @author     Nick Sagona, III <dev@noladev.com>
25 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    4.4.0
28 */
29class Manager extends AbstractManager
30{
31
32    /**
33     * Constructor
34     *
35     * Instantiate the module manager object.
36     *
37     * @param  ?array $modules
38     */
39    public function __construct(?array $modules = null)
40    {
41        if ($modules !== null) {
42            $this->registerModules($modules);
43        }
44    }
45
46    /**
47     * Register module objects
48     *
49     * @param  array $modules
50     * @return static
51     */
52    public function registerModules(array $modules): static
53    {
54        foreach ($modules as $module) {
55            $this->register($module);
56        }
57        return $this;
58    }
59
60    /**
61     * Register a module object
62     *
63     * @param  ModuleInterface $module
64     * @return static
65     */
66    public function register(ModuleInterface $module): static
67    {
68        return $this->addItem($module, $module->getName());
69    }
70
71    /**
72     * Determine if a module object is registered with the manager by $name
73     *
74     * @param  string $name
75     * @return bool
76     */
77    public function isRegistered(string $name): bool
78    {
79        return $this->hasItem($name);
80    }
81
82    /**
83     * Determine if a module object is registered with the manager by $module object comparison
84     *
85     * @param  ModuleInterface $module
86     * @return bool
87     */
88    public function hasModule(ModuleInterface $module): bool
89    {
90        $result = false;
91
92        foreach ($this->items as $name => $mod) {
93            if ($mod === $module) {
94                $result = true;
95                break;
96            }
97        }
98
99        return $result;
100    }
101
102    /**
103     * Get a module
104     *
105     * @param  string $name
106     * @return mixed
107     */
108    public function get(string $name): mixed
109    {
110        return $this->getItem($name);
111    }
112
113    /**
114     * Unregister a module
115     *
116     * @param  string $name
117     * @return static
118     */
119    public function unregister(string $name): static
120    {
121        return $this->removeItem($name);
122    }
123
124    /**
125     * Register a module with the manager
126     *
127     * @param  string $name
128     * @param  mixed $value
129     * @return void
130     */
131    public function __set(string $name, mixed $value): void
132    {
133        if (!($value instanceof ModuleInterface)) {
134            throw new InvalidArgumentException('Error: The value passed must be instance of ModuleInterface');
135        }
136        $value->setName($name);
137        $this->register($value);
138    }
139
140}