Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LogTablesProvider.php « Plugin « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2295501212a0938018efef6cea949cd87fd40867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugin;

use Piwik\Container\StaticContainer;
use Piwik\DataAccess\LogTableTemporary;
use Piwik\Piwik;
use Piwik\Tracker\LogTable;

class LogTablesProvider {

    /**
     * @var Manager
     */
    private $pluginManager;

    /**
     * @var LogTable[]
     */
    private $tablesCache;

    /**
     * @var LogTableTemporary
     */
    private $tempTable;

    public function __construct(Manager $pluginManager)
    {
        $this->pluginManager = $pluginManager;
    }

    /**
     * Get an instance of a specific log table if such a log table exists.
     *
     * @param string $tableNameWithoutPrefix  eg "log_visit"
     * @return LogTable|null
     */
    public function getLogTable($tableNameWithoutPrefix)
    {
        if ($this->tempTable && $this->tempTable->getName() === $tableNameWithoutPrefix) {
            return $this->tempTable;
        }
        foreach ($this->getAllLogTables() as $table) {
            if ($table->getName() === $tableNameWithoutPrefix) {
                return $table;
            }
        }
    }

    /**
     * @param LogTableTemporary|null $table
     */
    public function setTempTable($table)
    {
        $this->tempTable = $table;
    }

    public function clearCache()
    {
        $this->tablesCache = null;
    }

    /**
     * Needed for log query builder
     * @return LogTable[]
     */
    public function getAllLogTablesWithTemporary()
    {
        $tables = $this->getAllLogTables();
        if ($this->tempTable) {
            $tables[] = $this->tempTable;
        }
        return $tables;
    }

    /**
     * Get all log table instances defined by any activated and loaded plugin. The returned tables are not sorted in
     * any order.
     * @return LogTable[]
     */
    public function getAllLogTables()
    {
        if (!isset($this->tablesCache)) {
            $tables = $this->pluginManager->findMultipleComponents('Tracker', 'Piwik\\Tracker\\LogTable');

            $logTables = array();

            /**
             * Only used for tests. Triggered to add custom log tables which are not automatically picked up.
             * In case you need to define a log table, please put them inside the "Tracker" directory of your plugin.
             * Please note custom log tables are currently not an official API.
             *
             * **Example**
             *
             *     public function addLogTable(&$logTables)
             *     {
             *         $logTables[] = new LogTable();
             *     }
             *
             * @param array &$logTables An array containing a list of log entries.
             *
             * @internal Only used for tests
             * @ignore
             */
            Piwik::postEvent('LogTables.addLogTables', array(&$logTables));

            foreach ($tables as $table) {
                $logTables[] = StaticContainer::get($table);
            }

            $this->tablesCache = $logTables;
        }

        return $this->tablesCache;
    }

}