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