1<?php
2
3namespace ComboStrap;
4
5/**
6 *
7 *
8 */
9class EditButtonManager
10{
11
12
13    /**
14     * @var array Hold the actual edit button manager
15     */
16    private static $editButtonManagers;
17
18    /**
19     * @var EditButton[]
20     */
21    private $editButtonStack;
22
23    static function getOrCreate(): EditButtonManager
24    {
25
26        $page = MarkupPath::createFromRequestedPage();
27        $cacheKey = $page->getWikiId();
28        $editButtonManager = self::$editButtonManagers[$cacheKey] ?? null;
29        if ($editButtonManager === null) {
30            // new run, delete all old cache managers
31            self::$editButtonManagers = [];
32            // create
33            $editButtonManager = new EditButtonManager();
34            self::$editButtonManagers[$cacheKey] = $editButtonManager;
35        }
36        return $editButtonManager;
37    }
38
39    /**
40     * @param $name
41     * @param $startPosition
42     * @return EditButton
43     */
44    public function createAndAddEditButtonToStack($name, $startPosition): EditButton
45    {
46
47
48        if (empty($startPosition)) {
49            LogUtility::msg("The position for a start section should not be empty", LogUtility::LVL_MSG_ERROR, "support");
50        }
51        if (empty($name)) {
52            LogUtility::msg("The name for a start section should not be empty", LogUtility::LVL_MSG_ERROR, "support");
53        }
54
55        $editButton = EditButton::create($name)
56            ->setStartPosition($startPosition);
57        $this->editButtonStack[] = $editButton;
58        return $editButton;
59
60    }
61
62    public function popEditButtonFromStack($endPosition): ?EditButton
63    {
64        $editButton = array_pop($this->editButtonStack);
65        $editButton->setEndPosition($endPosition);
66        return $editButton;
67    }
68
69}
70