1<?php
2
3/**
4 * Recursion handler is responsible for preventing the recursive insertion
5 * of subpages to become an undending loop.
6 *
7 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
8 * @author  Adam Kučera <adam.kucera@wrent.cz>
9 */
10
11/**
12 * Class representing the Recurison Handlers
13 */
14class RecursionHandler {
15
16    /**
17     * An instance of Recursion Handler
18     * @var RecursionHandler
19     */
20    protected static $instance;
21    /**
22     * Array of all pages currently forbiden for recursion.
23     * @var array
24     */
25    protected $pages;
26
27    /**
28     * Handler is singleton, it is only accessible using this function.
29     * @return RecursionHandler
30     */
31    public static function getInstance() {
32        if(!isset(RecursionHandler::$instance)) {
33            RecursionHandler::$instance = new RecursionHandler();
34        }
35        return RecursionHandler::$instance;
36    }
37
38    /**
39     * Private constructor.
40     */
41    protected function __construct() {
42        $this->pages = array();
43    }
44
45    /**
46     * If the page is already in array, it can't be recursively inserted.
47     * @param string $page
48     * @return boolean
49     */
50    public function disallow($page) {
51        return in_array($page, $this->pages);
52    }
53
54    /**
55     * Inserts the page to array.
56     * @param string $page
57     */
58    public function insert($page) {
59        $this->pages[] = $page;
60    }
61
62    /**
63     * Removes the page from array and revalues the array.
64     * @param string $page
65     */
66    public function remove($page) {
67        $search = array_search($page, $this->pages);
68        if ($search !== FALSE) {
69            unset($this->pages[$search]);
70            $this->pages = array_values($this->pages);
71        }
72   }
73}
74