Skip to content

Commit 36fdd5d

Browse files
authored
Merge pull request #11 from 10up/fix/dont-instantiate-classes-not-extending-interface
Fix/dont instantiate classes not extending interface
2 parents e19e7c5 + e2a1651 commit 36fdd5d

File tree

4 files changed

+54
-41
lines changed

4 files changed

+54
-41
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Standalone Class
4+
*
5+
* @package TenupFrameworkTestClasses\Standalone
6+
*/
7+
8+
declare(strict_types = 1);
9+
10+
namespace TenupFrameworkTestClasses\Standalone;
11+
12+
/**
13+
* Standalone Class
14+
*
15+
* @package TenupFrameworkTestClasses\Standalone
16+
*/
17+
class Standalone {
18+
/**
19+
* Constructor.
20+
*/
21+
public function __construct() {
22+
add_action( 'init', [ $this, 'init' ] );
23+
}
24+
25+
/**
26+
* Init method.
27+
*
28+
* @return void
29+
*/
30+
public function init() {
31+
echo 'Hello from the Standalone class!';
32+
}
33+
}

src/ModuleInitialization.php

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,17 @@ public function init_classes( $dir = '' ) {
135135
continue;
136136
}
137137

138+
// Check if the class implements ModuleInterface before instantiating it
139+
if ( ! $reflection_class->implementsInterface( 'TenupFramework\ModuleInterface' ) ) {
140+
continue;
141+
}
142+
138143
// Initialize the class.
139144
// phpcs:ignore Generic.Commenting.DocComment.MissingShort
140145
/** @var ModuleInterface $instantiated_class */
141146
$instantiated_class = new $class();
142147

143-
// Make sure the class is a subclass of Module, so we can initialize it.
144-
if ( ! in_array( 'TenupFramework\Module', $this->class_uses_recursive( $instantiated_class ), true ) ) {
145-
unset( $instantiated_class );
146-
continue;
147-
}
148+
do_action( 'tenup_framework_module_init__' . $slug, $instantiated_class );
148149

149150
// Assign the classes into the order they should be initialized.
150151
$load_class_order[ intval( $instantiated_class->load_order() ) ][] = [
@@ -173,42 +174,6 @@ public function init_classes( $dir = '' ) {
173174
}
174175
}
175176

176-
/**
177-
* Returns all traits used by a class and its traits.
178-
*
179-
* @param object|string $class_to_search The class to check for traits.
180-
* @return array<string>
181-
*/
182-
protected function class_uses_recursive( $class_to_search ) {
183-
if ( is_object( $class_to_search ) ) {
184-
$class_to_search = get_class( $class_to_search );
185-
}
186-
187-
$results = [];
188-
189-
foreach ( array_reverse( class_parents( $class_to_search ) ? class_parents( $class_to_search ) : [] ) + [ $class_to_search => $class_to_search ] as $class ) {
190-
$results = array_merge( $results, $this->trait_uses_recursive( $class ) );
191-
}
192-
193-
return array_unique( $results );
194-
}
195-
196-
/**
197-
* Returns all traits used by a trait and its traits.
198-
*
199-
* @param object|string $trait_to_search The trait to check for traits.
200-
* @return array<string>
201-
*/
202-
protected function trait_uses_recursive( $trait_to_search ) {
203-
$traits_to_search = class_uses( $trait_to_search ) ? class_uses( $trait_to_search ) : [];
204-
205-
foreach ( $traits_to_search as $trait ) {
206-
$traits_to_search = array_merge( $traits_to_search, $this->trait_uses_recursive( $trait ) );
207-
}
208-
209-
return (array) $traits_to_search;
210-
}
211-
212177
/**
213178
* Slugify a class name.
214179
*

tests/FrameworkTestSetup.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
/**
2121
* Trait FrameworkTestSetup
2222
*
23+
* @runTestsInSeparateProcesses
24+
*
2325
* @package TenupFramework
2426
*/
2527
trait FrameworkTestSetup {

tests/ModuleInitializationTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,17 @@ public function test_get_module_returns_instantiated_class() {
121121
$module = \TenupFramework\ModuleInitialization::get_module( 'TenupFrameworkTestClasses\DoesntExist' );
122122
$this->assertFalse( $module );
123123
}
124+
125+
/**
126+
* Test that only classes implementing ModuleInterface are initialized.
127+
*
128+
* @return void
129+
*/
130+
public function test_only_classes_implementing_module_interface_are_initialized() {
131+
$module_init = \TenupFramework\ModuleInitialization::instance();
132+
$module_init->init_classes( dirname( __DIR__, 1 ) . '/fixtures/classes' );
133+
134+
$this->assertTrue( did_action( 'tenup_framework_module_init__tenupframeworktestclasses-posttypes-demo' ) > 0, 'Demo was not initialized.' );
135+
$this->assertFalse( did_action( 'tenup_framework_module_init__tenupframeworktestclasses-standalone-standalone' ) > 0, 'Standalone class was initialized.' );
136+
}
124137
}

0 commit comments

Comments
 (0)