Exception.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Exception;
  36. use Hoa\Consistency;
  37. use Hoa\Event;
  38. /**
  39. * Class \Hoa\Exception\Exception.
  40. *
  41. * Each exception must extend \Hoa\Exception\Exception.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Exception extends Idle implements Event\Source
  47. {
  48. /**
  49. * Create an exception.
  50. * An exception is built with a formatted message, a code (an ID), and an
  51. * array that contains the list of formatted string for the message. If
  52. * chaining, we can add a previous exception.
  53. *
  54. * @param string $message Formatted message.
  55. * @param int $code Code (the ID).
  56. * @param array $arguments Arguments to format message.
  57. * @param \Throwable $previous Previous exception in chaining.
  58. */
  59. public function __construct(
  60. $message,
  61. $code = 0,
  62. $arguments = [],
  63. $previous = null
  64. ) {
  65. parent::__construct($message, $code, $arguments, $previous);
  66. if (false === Event::eventExists('hoa://Event/Exception')) {
  67. Event::register('hoa://Event/Exception', __CLASS__);
  68. }
  69. $this->send();
  70. return;
  71. }
  72. /**
  73. * Send the exception on hoa://Event/Exception.
  74. *
  75. * @return void
  76. */
  77. public function send()
  78. {
  79. Event::notify(
  80. 'hoa://Event/Exception',
  81. $this,
  82. new Event\Bucket($this)
  83. );
  84. return;
  85. }
  86. }
  87. /**
  88. * Flex entity.
  89. */
  90. Consistency::flexEntity('Hoa\Exception\Exception');