1<?php
2/*
3 * Yurii's Gantt Plugin
4 *
5 * Copyright (C) 2020 Yurii K.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see http://www.gnu.org/licenses
19 */
20
21namespace dokuwiki\plugin\yuriigantt\src\Driver\Embedded;
22
23use \dokuwiki\Extension\SyntaxPlugin;
24
25//
26// WORKAROUND: for stable version
27//
28if (!class_exists(\dokuwiki\Extension\SyntaxPlugin::class)) {
29    class_alias(\Doku_Handler::class, \dokuwiki\Extension\SyntaxPlugin::class);
30}
31
32/**
33 * We mimic \Doku_Handler without unnecessary code and some changes
34 */
35final class Handler extends \Doku_Handler // NOTE: remove extend when PHP 5.6-7.1 support is dropped
36{
37
38    protected $database;
39    /** @var CallWriter */
40    protected $callWriter;
41    /** @var array */
42    public $calls = [];
43
44
45    public function __construct()
46    {
47        $this->callWriter = new CallWriter($this);
48        $this->calls = [];
49    }
50
51
52    public function setDatabase($database)
53    {
54        //TODO: checks
55        $this->database = $database;
56    }
57
58
59    public function getDatabase()
60    {
61        return $this->database;
62    }
63
64
65    /**
66     * Processing function. Used by Lexer during parse.
67     * We make sure other page content will be in place
68     *
69     * @param string $content
70     * @param int $state
71     * @param int $pos
72     * @return bool
73     */
74    public function embedded($content, $state, $pos)
75    {
76        if ($state === DOKU_LEXER_UNMATCHED) {
77            $this->callWriter->writeCall(['raw', [$content], $pos]);
78            return true;
79        }
80
81        return false;
82    }
83
84
85    /**
86     * Special plugin handler
87     *
88     * This handler is called for all modes starting with 'plugin_'.
89     * An additional parameter with the plugin name is passed. The plugin's handle()
90     * method is called here
91     *
92     * @param string $match matched syntax
93     * @param int $state a LEXER_STATE_* constant
94     * @param int $pos byte position in the original source file
95     * @param string $pluginname name of the plugin
96     * @return bool mode handled?
97     * @author Andreas Gohr <andi@splitbrain.org>
98     *
99     */
100    public function plugin($match, $state, $pos, $pluginname)
101    {
102        $data = array($match);
103        /** @var SyntaxPlugin $plugin */
104        $plugin = plugin_load('syntax', $pluginname);
105        if ($plugin != null) {
106            $data = $plugin->handle($match, $state, $pos, $this);
107        }
108        if ($data !== false) {
109            $this->addPluginCall($pluginname, $data, $state, $pos, $match);
110        }
111        return true;
112    }
113
114
115    /**
116     * Similar to addCall, but adds a plugin call
117     *
118     * @param string $plugin name of the plugin
119     * @param mixed $args arguments for this call
120     * @param int $state a LEXER_STATE_* constant
121     * @param int $pos byte position in the original source file
122     * @param string $match matched syntax
123     */
124    public function addPluginCall($plugin, $args, $state, $pos, $match)
125    {
126        $call = array('plugin', array($plugin, $args, $state, $match), $pos);
127        $this->callWriter->writeCall($call);
128    }
129}
130