Skip to content

Commit aeab7ba

Browse files
committed
add register kernel listeners&&ServiceProvider wrapper
1 parent a181576 commit aeab7ba

1 file changed

Lines changed: 100 additions & 1 deletion

File tree

src/Framework/Traits/KernelTrait.php

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
<?php
2+
23
namespace TastPHP\Framework\Traits;
34

5+
use TastPHP\Framework\Cache\CacheServiceProvider;
6+
use TastPHP\Framework\Cache\FileCacheServiceProvider;
7+
use TastPHP\Framework\CsrfToken\CsrfTokenServiceProvider;
8+
use TastPHP\Framework\Doctrine\DoctrineServiceProvider;
9+
use TastPHP\Framework\Event\MailEvent;
410
use TastPHP\Framework\Handler\AliasLoaderHandler;
511
use TastPHP\Framework\Event\AppEvent;
12+
use TastPHP\Framework\Jwt\JwtServiceProvider;
13+
use TastPHP\Framework\Logger\LoggerServiceProvider;
14+
use TastPHP\Framework\Queue\QueueServiceProvider;
15+
use TastPHP\Framework\SwiftMailer\SwiftMailerServiceProvider;
16+
use TastPHP\Framework\Twig\TwigServiceProvider;
617

718
/**
819
* Class KernelTrait
@@ -15,7 +26,7 @@ trait KernelTrait
1526
*/
1627
public function run()
1728
{
18-
$httpEvent = $this['eventDispatcher']->dispatch(AppEvent::RESPONSE, new \TastPHP\Framework\Event\HttpEvent(null, $this['router']->matchCurrentRequest(),$this));
29+
$httpEvent = $this['eventDispatcher']->dispatch(AppEvent::RESPONSE, new \TastPHP\Framework\Event\HttpEvent(null, $this['router']->matchCurrentRequest(), $this));
1930
if (!empty($this['swoole'])) {
2031
return $httpEvent;
2132
}
@@ -146,4 +157,92 @@ public function getListeners($key = '')
146157

147158
return $this->listeners;
148159
}
160+
161+
//ServiceProvider register
162+
163+
protected function registerRedisService()
164+
{
165+
$this->replaceServiceProvider("Redis", RedisServiceProvider::class);
166+
}
167+
168+
protected function registerCacheService()
169+
{
170+
$this->replaceServiceProvider("Cache", CacheServiceProvider::class);
171+
}
172+
173+
protected function registerFileCacheService()
174+
{
175+
$this->replaceServiceProvider("FileCache", FileCacheServiceProvider::class);
176+
}
177+
178+
protected function registerLoggerService()
179+
{
180+
$this->replaceServiceProvider("Logger", LoggerServiceProvider::class);
181+
}
182+
183+
protected function registerTwigService()
184+
{
185+
$this->replaceServiceProvider("Twig", TwigServiceProvider::class);
186+
}
187+
188+
protected function registerDoctrineService()
189+
{
190+
$this->replaceServiceProvider("Doctrine", DoctrineServiceProvider::class);
191+
}
192+
193+
protected function registerCsrfTokenService()
194+
{
195+
$this->replaceServiceProvider("CsrfToken", CsrfTokenServiceProvider::class);
196+
}
197+
198+
protected function registerJwtService()
199+
{
200+
$this->replaceServiceProvider("Jwt", JwtServiceProvider::class);
201+
}
202+
203+
protected function registerSwiftMailerService()
204+
{
205+
$this->replaceServiceProvider("SwiftMailer", SwiftMailerServiceProvider::class);
206+
}
207+
208+
protected function registerQueueService()
209+
{
210+
$this->replaceServiceProvider("Queue", QueueServiceProvider::class);
211+
}
212+
213+
// kernel listener register
214+
215+
/**
216+
* @param $listener
217+
* @param string $action
218+
*/
219+
protected function registerRequestListener($listener, $action = "onRequestAction")
220+
{
221+
$this->replaceListener(AppEvent::REQUEST, $listener, $action);
222+
}
223+
224+
protected function registerMiddlewareListener($listener, $action = "onMiddlewareAction")
225+
{
226+
$this->replaceListener(AppEvent::MIDDLEWARE, $listener, $action);
227+
}
228+
229+
protected function registerResponseListener($listener, $action = "onResponseAction")
230+
{
231+
$this->replaceListener(AppEvent::RESPONSE, $listener, $action);
232+
}
233+
234+
protected function registerExceptionListener($listener, $action = "onExceptionAction")
235+
{
236+
$this->replaceListener(AppEvent::EXCEPTION, $listener, $action);
237+
}
238+
239+
protected function registerMailListener($listener, $action = "onSendMailAction")
240+
{
241+
$this->replaceListener(MailEvent::MAIlSEND, $listener, $action);
242+
}
243+
244+
protected function registerNotFoundListener($listener, $action = "onNotFoundPageAction")
245+
{
246+
$this->replaceListener(AppEvent::NOTFOUND, $listener, $action);
247+
}
149248
}

0 commit comments

Comments
 (0)