Events.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Config;
  3. use CodeIgniter\Events\Events;
  4. use CodeIgniter\Exceptions\FrameworkException;
  5. use CodeIgniter\HotReloader\HotReloader;
  6. /*
  7. * --------------------------------------------------------------------
  8. * Application Events
  9. * --------------------------------------------------------------------
  10. * Events allow you to tap into the execution of the program without
  11. * modifying or extending core files. This file provides a central
  12. * location to define your events, though they can always be added
  13. * at run-time, also, if needed.
  14. *
  15. * You create code that can execute by subscribing to events with
  16. * the 'on()' method. This accepts any form of callable, including
  17. * Closures, that will be executed when the event is triggered.
  18. *
  19. * Example:
  20. * Events::on('create', [$myInstance, 'myMethod']);
  21. */
  22. Events::on('pre_system', static function (): void {
  23. if (ENVIRONMENT !== 'testing') {
  24. if (ini_get('zlib.output_compression')) {
  25. throw FrameworkException::forEnabledZlibOutputCompression();
  26. }
  27. while (ob_get_level() > 0) {
  28. ob_end_flush();
  29. }
  30. ob_start(static fn ($buffer) => $buffer);
  31. }
  32. /*
  33. * --------------------------------------------------------------------
  34. * Debug Toolbar Listeners.
  35. * --------------------------------------------------------------------
  36. * If you delete, they will no longer be collected.
  37. */
  38. if (CI_DEBUG && ! is_cli()) {
  39. Events::on('DBQuery', 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect');
  40. Services::toolbar()->respond();
  41. // Hot Reload route - for framework use on the hot reloader.
  42. if (ENVIRONMENT === 'development') {
  43. Services::routes()->get('__hot-reload', static function (): void {
  44. (new HotReloader())->run();
  45. });
  46. }
  47. }
  48. });