]> git.mxchange.org Git - friendica.git/blob - src/App/Mode.php
Revert "Introduce Arguments / Module class"
[friendica.git] / src / App / Mode.php
1 <?php
2
3 namespace Friendica\App;
4
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Database\Database;
7 use Friendica\Util\BasePath;
8
9 /**
10  * Mode of the current Friendica Node
11  *
12  * @package Friendica\App
13  */
14 class Mode
15 {
16         const LOCALCONFIGPRESENT  = 1;
17         const DBAVAILABLE         = 2;
18         const DBCONFIGAVAILABLE   = 4;
19         const MAINTENANCEDISABLED = 8;
20
21         /***
22          * @var int the mode of this Application
23          *
24          */
25         private $mode;
26
27         /**
28          * @var string the basepath of the application
29          */
30         private $basepath;
31
32         /**
33          * @var Database
34          */
35         private $database;
36
37         /**
38          * @var ConfigCache
39          */
40         private $configCache;
41
42         public function __construct(BasePath $basepath, Database $database, ConfigCache $configCache)
43         {
44                 $this->basepath    = $basepath->getPath();
45                 $this->database    = $database;
46                 $this->configCache = $configCache;
47                 $this->mode        = 0;
48         }
49
50         /**
51          * Sets the App mode
52          *
53          * - App::MODE_INSTALL    : Either the database connection can't be established or the config table doesn't exist
54          * - App::MODE_MAINTENANCE: The maintenance mode has been set
55          * - App::MODE_NORMAL     : Normal run with all features enabled
56          *
57          * @param string $basePath the Basepath of the Application
58          *
59          * @return Mode returns itself
60          *
61          * @throws \Exception
62          */
63         public function determine($basePath = null)
64         {
65                 if (!empty($basePath)) {
66                         $this->basepath = $basePath;
67                 }
68
69                 $this->mode = 0;
70
71                 if (!file_exists($this->basepath . '/config/local.config.php')
72                     && !file_exists($this->basepath . '/config/local.ini.php')
73                     && !file_exists($this->basepath . '/.htconfig.php')) {
74                         return $this;
75                 }
76
77                 $this->mode |= Mode::LOCALCONFIGPRESENT;
78
79                 if (!$this->database->connected()) {
80                         return $this;
81                 }
82
83                 $this->mode |= Mode::DBAVAILABLE;
84
85                 if ($this->database->fetchFirst("SHOW TABLES LIKE 'config'") === false) {
86                         return $this;
87                 }
88
89                 $this->mode |= Mode::DBCONFIGAVAILABLE;
90
91                 if (!empty($this->configCache->get('system', 'maintenance')) ||
92                     // Don't use Config or Configuration here because we're possibly BEFORE initializing the Configuration,
93                     // so this could lead to a dependency circle
94                     !empty($this->database->selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])['v'])) {
95                         return $this;
96                 }
97
98                 $this->mode |= Mode::MAINTENANCEDISABLED;
99
100                 return $this;
101         }
102
103         /**
104          * Checks, if the Friendica Node has the given mode
105          *
106          * @param int $mode A mode to test
107          *
108          * @return bool returns true, if the mode is set
109          */
110         public function has($mode)
111         {
112                 return ($this->mode & $mode) > 0;
113         }
114
115
116         /**
117          * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
118          *
119          * @return bool
120          */
121         public function isInstall()
122         {
123                 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
124                        !$this->has(MODE::DBCONFIGAVAILABLE);
125         }
126
127         /**
128          * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
129          *
130          * @return bool
131          */
132         public function isNormal()
133         {
134                 return $this->has(Mode::LOCALCONFIGPRESENT) &&
135                        $this->has(Mode::DBAVAILABLE) &&
136                        $this->has(Mode::DBCONFIGAVAILABLE) &&
137                        $this->has(Mode::MAINTENANCEDISABLED);
138         }
139 }