Skip to content

Commit 4de07c3

Browse files
committed
Merge branch 'release/0.7.4'
2 parents d711599 + fd5439e commit 4de07c3

File tree

4 files changed

+124
-27
lines changed

4 files changed

+124
-27
lines changed

.version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"strategy": "semver",
33
"major": 0,
44
"minor": 7,
5-
"patch": 3,
5+
"patch": 4,
66
"build": 0
77
}

VERSIONLOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.7.4 2025-11-21
2+
* Added get and set magic methods to the Registry.
3+
14
## 0.7.3 2025-11-11
25
* Converted to camelcase.
36

src/Patterns/Registry.php

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,31 @@
1010
* throughout the application lifecycle. It serves as a service locator and
1111
* dependency injection container, allowing components to share state and
1212
* 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+
*
3214
* @package Neuron\Patterns
3315
*
3416
* @example
3517
* ```php
3618
* $registry = Registry::instance();
37-
*
38-
* // Store configuration
19+
*
20+
* // Store configuration (method syntax)
3921
* $registry->set('database.config', $dbConfig);
4022
* $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)
4329
* $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+
*
4637
* // Cleanup
47-
* $registry->remove('temp.data');
4838
* $registry->reset(); // Clear all objects
4939
* ```
5040
*/
@@ -87,4 +77,38 @@ public function reset() : void
8777
{
8878
$this->_objects = [];
8979
}
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+
}
90114
}

tests/Patterns/RegistryTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,74 @@ public function testReset()
4242

4343
$this->assertNull( $Reg2->get( 'test' ) );
4444
}
45+
46+
public function testMagicSet()
47+
{
48+
$Reg1 = Registry::getInstance();
49+
$Reg1->reset();
50+
51+
// Test magic __set
52+
$Reg1->testKey = 'magic value';
53+
54+
// Verify using regular get method
55+
$this->assertEquals( 'magic value', $Reg1->get( 'testKey' ) );
56+
57+
// Verify using getInstance
58+
$Reg2 = Registry::getInstance();
59+
$this->assertEquals( 'magic value', $Reg2->get( 'testKey' ) );
60+
}
61+
62+
public function testMagicGet()
63+
{
64+
$Reg1 = Registry::getInstance();
65+
$Reg1->reset();
66+
67+
// Set using regular method
68+
$Reg1->set( 'testKey', 'test value' );
69+
70+
// Test magic __get
71+
$this->assertEquals( 'test value', $Reg1->testKey );
72+
73+
// Verify null is returned for non-existent keys
74+
$this->assertNull( $Reg1->nonExistentKey );
75+
}
76+
77+
public function testMagicIsset()
78+
{
79+
$Reg1 = Registry::getInstance();
80+
$Reg1->reset();
81+
82+
// Test isset on non-existent key
83+
$this->assertFalse( isset( $Reg1->testKey ) );
84+
85+
// Set a value
86+
$Reg1->testKey = 'some value';
87+
88+
// Test isset on existing key
89+
$this->assertTrue( isset( $Reg1->testKey ) );
90+
91+
// Reset and verify isset returns false
92+
$Reg1->reset();
93+
$this->assertFalse( isset( $Reg1->testKey ) );
94+
}
95+
96+
public function testMagicMethodsIntegration()
97+
{
98+
$Reg1 = Registry::getInstance();
99+
$Reg1->reset();
100+
101+
// Mix magic methods and regular methods
102+
$Reg1->magicKey = 'magic';
103+
$Reg1->set( 'regularKey', 'regular' );
104+
105+
// Verify both can be retrieved with either syntax
106+
$this->assertEquals( 'magic', $Reg1->magicKey );
107+
$this->assertEquals( 'magic', $Reg1->get( 'magicKey' ) );
108+
$this->assertEquals( 'regular', $Reg1->regularKey );
109+
$this->assertEquals( 'regular', $Reg1->get( 'regularKey' ) );
110+
111+
// Verify isset works for both
112+
$this->assertTrue( isset( $Reg1->magicKey ) );
113+
$this->assertTrue( isset( $Reg1->regularKey ) );
114+
}
45115
}

0 commit comments

Comments
 (0)