GracefullTerminationException.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Licensed to Jasig under one or more contributor license
  4. * agreements. See the NOTICE file distributed with this work for
  5. * additional information regarding copyright ownership.
  6. *
  7. * Jasig licenses this file to you under the Apache License,
  8. * Version 2.0 (the "License"); you may not use this file except in
  9. * compliance with the License. You may obtain a copy of the License at:
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * PHP Version 5
  20. *
  21. * @file CAS/GracefullTerminationException.php
  22. * @category Authentication
  23. * @package PhpCAS
  24. * @author Joachim Fritschi <jfritschi@freenet.de>
  25. * @author Adam Franco <afranco@middlebury.edu>
  26. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  27. * @link https://wiki.jasig.org/display/CASC/phpCAS
  28. */
  29. /**
  30. * An exception for terminatinating execution or to throw for unit testing
  31. *
  32. * @class CAS_GracefullTerminationException.php
  33. * @category Authentication
  34. * @package PhpCAS
  35. * @author Joachim Fritschi <jfritschi@freenet.de>
  36. * @author Adam Franco <afranco@middlebury.edu>
  37. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  38. * @link https://wiki.jasig.org/display/CASC/phpCAS
  39. */
  40. class CAS_GracefullTerminationException
  41. extends RuntimeException
  42. implements CAS_Exception
  43. {
  44. /**
  45. * Test if exceptions should be thrown or if we should just exit.
  46. * In production usage we want to just exit cleanly when prompting the user
  47. * for a redirect without filling the error logs with uncaught exceptions.
  48. * In unit testing scenarios we cannot exit or we won't be able to continue
  49. * with our tests.
  50. *
  51. * @param string $message Message Text
  52. * @param int $code Error code
  53. *
  54. * @return self
  55. */
  56. public function __construct ($message = 'Terminate Gracefully', $code = 0)
  57. {
  58. // Exit cleanly to avoid filling up the logs with uncaught exceptions.
  59. if (self::$_exitWhenThrown) {
  60. exit;
  61. } else {
  62. // Throw exceptions to allow unit testing to continue;
  63. parent::__construct($message, $code);
  64. }
  65. }
  66. private static $_exitWhenThrown = true;
  67. /**
  68. * Force phpcas to thow Exceptions instead of calling exit()
  69. * Needed for unit testing. Generally shouldn't be used in production due to
  70. * an increase in Apache error logging if CAS_GracefulTerminiationExceptions
  71. * are not caught and handled.
  72. *
  73. * @return void
  74. */
  75. public static function throwInsteadOfExiting()
  76. {
  77. self::$_exitWhenThrown = false;
  78. }
  79. }
  80. ?>