File tree 3 files changed +49
-1
lines changed
user_guide_src/source/extending
3 files changed +49
-1
lines changed Original file line number Diff line number Diff line change 14
14
namespace CodeIgniter \CLI ;
15
15
16
16
use CodeIgniter \Autoloader \FileLocatorInterface ;
17
+ use CodeIgniter \Events \Events ;
17
18
use CodeIgniter \Log \Logger ;
18
19
use ReflectionClass ;
19
20
use ReflectionException ;
@@ -64,7 +65,13 @@ public function run(string $command, array $params)
64
65
$ className = $ this ->commands [$ command ]['class ' ];
65
66
$ class = new $ className ($ this ->logger , $ this );
66
67
67
- return $ class ->run ($ params );
68
+ Events::trigger ('pre_command ' );
69
+
70
+ $ exit = $ class ->run ($ params );
71
+
72
+ Events::trigger ('post_command ' );
73
+
74
+ return $ exit ;
68
75
}
69
76
70
77
/**
Original file line number Diff line number Diff line change 15
15
16
16
use CodeIgniter \CodeIgniter ;
17
17
use CodeIgniter \Config \DotEnv ;
18
+ use CodeIgniter \Events \Events ;
18
19
use CodeIgniter \Test \CIUnitTestCase ;
19
20
use CodeIgniter \Test \Mock \MockCLIConfig ;
20
21
use CodeIgniter \Test \Mock \MockCodeIgniter ;
@@ -79,6 +80,38 @@ public function testRun(): void
79
80
$ this ->assertStringContainsString ('Displays basic usage information. ' , $ this ->getStreamFilterBuffer ());
80
81
}
81
82
83
+ public function testRunEventsPreCommand (): void
84
+ {
85
+ $ result = '' ;
86
+ Events::on ('pre_command ' , static function () use (&$ result ): void {
87
+ $ result = 'fired ' ;
88
+ });
89
+
90
+ $ this ->initCLI ();
91
+
92
+ $ console = new Console ();
93
+ $ console ->run ();
94
+
95
+ $ this ->assertEventTriggered ('pre_command ' );
96
+ $ this ->assertSame ('fired ' , $ result );
97
+ }
98
+
99
+ public function testRunEventsPostCommand (): void
100
+ {
101
+ $ result = '' ;
102
+ Events::on ('post_command ' , static function () use (&$ result ): void {
103
+ $ result = 'fired ' ;
104
+ });
105
+
106
+ $ this ->initCLI ();
107
+
108
+ $ console = new Console ();
109
+ $ console ->run ();
110
+
111
+ $ this ->assertEventTriggered ('post_command ' );
112
+ $ this ->assertSame ('fired ' , $ result );
113
+ }
114
+
82
115
public function testBadCommand (): void
83
116
{
84
117
$ this ->initCLI ('bogus ' );
Original file line number Diff line number Diff line change @@ -100,6 +100,14 @@ invoked by **public/index.php**:
100
100
* **post_system ** Called right before the final rendered page is sent to the browser,
101
101
at the end of system execution, after the execution of "after" controller filters.
102
102
103
+ For CLI Apps
104
+ ------------
105
+
106
+ The following is a list of available event points for :doc: `../cli/spark_commands `:
107
+
108
+ * **pre_command ** Called right before the command code execution.
109
+ * **post_command ** Called right after the command code execution.
110
+
103
111
Others
104
112
------
105
113
You can’t perform that action at this time.
0 commit comments