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