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

Factory.php « Storage « Settings « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c1c6ca6456a40e0c4fa0097c1fe8bfe9e36b534 (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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Settings\Storage;

use Piwik\Settings\Storage\Backend\BackendInterface;
use Piwik\SettingsServer;

/**
 * Factory to create an instance of a storage. The storage can be created with different backends depending on the need.
 *
 * @package Piwik\Settings\Storage
 */
class Factory
{
    // cache prevents multiple loading of storage
    private $cache = array();

    /**
     * Get a storage instance for plugin settings.
     *
     * The storage will hold values that belong to the given plugin name and user login. Be aware that instances
     * for a specific plugin and login will be cached during one request for better performance.
     *
     * @param string $pluginName
     * @param string $userLogin Use an empty string if settings should be not for a specific login
     * @return Storage
     */
    public function getPluginStorage($pluginName, $userLogin)
    {
        $id = $pluginName . '#' . $userLogin;

        if (!isset($this->cache[$id])) {
            $backend = new Backend\PluginSettingsTable($pluginName, $userLogin);
            $this->cache[$id] = $this->makeStorage($backend);
        }

        return $this->cache[$id];
    }

    /**
     * Get a storage instance for measurable settings.
     *
     * The storage will hold values that belong to the given idSite and plugin name. Be aware that a storage instance
     * for a specific site and plugin will be cached during one request for better performance.
     *
     * @param int $idSite   If idSite is empty it will use a backend that never actually persists any value. Pass
     *                      $idSite = 0 to create a storage for a site that is about to be created.
     * @param string $pluginName
     * @return Storage
     */
    public function getMeasurableSettingsStorage($idSite, $pluginName)
    {
        $id = 'measurableSettings' . (int) $idSite . '#' . $pluginName;

        if (empty($idSite)) {
            return $this->getNonPersistentStorage($id . '#nonpersistent');
        }

        if (!isset($this->cache[$id])) {
            $backend = new Backend\MeasurableSettingsTable($idSite, $pluginName);
            $this->cache[$id] = $this->makeStorage($backend);
        }

        return $this->cache[$id];
    }

    /**
     * Get a storage instance for settings that will be saved in the "site" table.
     *
     * The storage will hold values that belong to the given idSite. Be aware that a storage instance for a specific
     * site will be cached during one request for better performance.
     *
     * @param int $idSite   If idSite is empty it will use a backend that never actually persists any value. Pass
     *                      $idSite = 0 to create a storage for a site that is about to be created.
     *
     * @param int $idSite
     * @return Storage
     */
    public function getSitesTable($idSite)
    {
        $id = 'sitesTable#' . $idSite;

        if (empty($idSite)) {
            return $this->getNonPersistentStorage($id . '#nonpersistent');
        }

        if (!isset($this->cache[$id])) {
            $backend = new Backend\SitesTable($idSite);
            $this->cache[$id] = $this->makeStorage($backend);
        }

        return $this->cache[$id];
    }

    /**
     * Get a storage with a backend that will never persist or load any value.
     *
     * @param string $key
     * @return Storage
     */
    public function getNonPersistentStorage($key)
    {
        return new Storage(new Backend\Null($key));
    }

    private function makeStorage(BackendInterface $backend)
    {
        if (SettingsServer::isTrackerApiRequest()) {
            $backend = new Backend\Cache($backend);
        }

        return new Storage($backend);
    }
}