Events.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. $value = ini_get('zlib.output_compression');
  25. if (filter_var($value, FILTER_VALIDATE_BOOLEAN) || (int) $value > 0) {
  26. throw FrameworkException::forEnabledZlibOutputCompression();
  27. }
  28. while (ob_get_level() > 0) {
  29. ob_end_flush();
  30. }
  31. ob_start(static fn ($buffer) => $buffer);
  32. }
  33. /*
  34. * --------------------------------------------------------------------
  35. * Debug Toolbar Listeners.
  36. * --------------------------------------------------------------------
  37. * If you delete, they will no longer be collected.
  38. */
  39. if (CI_DEBUG && ! is_cli()) {
  40. Events::on('DBQuery', 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect');
  41. service('toolbar')->respond();
  42. // Hot Reload route - for framework use on the hot reloader.
  43. if (ENVIRONMENT === 'development') {
  44. service('routes')->get('__hot-reload', static function (): void {
  45. (new HotReloader())->run();
  46. });
  47. }
  48. }
  49. });