|
10 | 10 | * throughout the application lifecycle. It serves as a service locator and |
11 | 11 | * dependency injection container, allowing components to share state and |
12 | 12 | * communicate across the entire framework. |
13 | | - * |
14 | | - * Key characteristics: |
15 | | - * - Thread-safe singleton implementation |
16 | | - * - Global state management for framework components |
17 | | - * - Object lifecycle management and storage |
18 | | - * - Service locator pattern for dependency resolution |
19 | | - * - Memory-based storage with automatic cleanup |
20 | | - * |
21 | | - * Usage patterns: |
22 | | - * - Store framework configuration objects |
23 | | - * - Cache expensive-to-create objects (database connections, etc.) |
24 | | - * - Share state between disconnected components |
25 | | - * - Implement service locator for dependency injection |
26 | | - * |
27 | | - * Thread Safety: |
28 | | - * The registry uses memory-based singleton storage, which is safe for |
29 | | - * single-threaded PHP environments. For multi-threaded scenarios, |
30 | | - * consider using alternative storage backends. |
31 | | - * |
| 13 | + * |
32 | 14 | * @package Neuron\Patterns |
33 | 15 | * |
34 | 16 | * @example |
35 | 17 | * ```php |
36 | 18 | * $registry = Registry::instance(); |
37 | | - * |
38 | | - * // Store configuration |
| 19 | + * |
| 20 | + * // Store configuration (method syntax) |
39 | 21 | * $registry->set('database.config', $dbConfig); |
40 | 22 | * $registry->set('app.settings', $appSettings); |
41 | | - * |
42 | | - * // Retrieve objects |
| 23 | + * |
| 24 | + * // Store configuration (property syntax using magic methods) |
| 25 | + * $registry->databaseConfig = $dbConfig; |
| 26 | + * $registry->appSettings = $appSettings; |
| 27 | + * |
| 28 | + * // Retrieve objects (method syntax) |
43 | 29 | * $dbConfig = $registry->get('database.config'); |
44 | | - * $hasCache = $registry->has('cache.instance'); |
45 | | - * |
| 30 | + * |
| 31 | + * // Retrieve objects (property syntax using magic methods) |
| 32 | + * $dbConfig = $registry->databaseConfig; |
| 33 | + * |
| 34 | + * // Check existence |
| 35 | + * $exists = isset($registry->databaseConfig); |
| 36 | + * |
46 | 37 | * // Cleanup |
47 | | - * $registry->remove('temp.data'); |
48 | 38 | * $registry->reset(); // Clear all objects |
49 | 39 | * ``` |
50 | 40 | */ |
@@ -87,4 +77,38 @@ public function reset() : void |
87 | 77 | { |
88 | 78 | $this->_objects = []; |
89 | 79 | } |
| 80 | + |
| 81 | + /** |
| 82 | + * Magic method to get a registry value using property syntax. |
| 83 | + * |
| 84 | + * @param string $name The name of the registry key |
| 85 | + * @return mixed The value stored in the registry, or null if not found |
| 86 | + */ |
| 87 | + public function __get( string $name ) : mixed |
| 88 | + { |
| 89 | + return $this->get( $name ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Magic method to set a registry value using property syntax. |
| 94 | + * |
| 95 | + * @param string $name The name of the registry key |
| 96 | + * @param mixed $value The value to store in the registry |
| 97 | + * @return void |
| 98 | + */ |
| 99 | + public function __set( string $name, mixed $value ) : void |
| 100 | + { |
| 101 | + $this->set( $name, $value ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Magic method to check if a registry key exists using isset(). |
| 106 | + * |
| 107 | + * @param string $name The name of the registry key |
| 108 | + * @return bool True if the key exists in the registry, false otherwise |
| 109 | + */ |
| 110 | + public function __isset( string $name ) : bool |
| 111 | + { |
| 112 | + return array_key_exists( $name, $this->_objects ); |
| 113 | + } |
90 | 114 | } |
0 commit comments