]> git.mxchange.org Git - friendica.git/blob - src/App/Mode.php
Merge pull request #7508 from nupplaphil/task/arguments_module_class_2
[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         public function __construct(int $mode = 0)
28         {
29                 $this->mode = $mode;
30         }
31
32         /**
33          * Sets the App mode
34          *
35          * - App::MODE_INSTALL    : Either the database connection can't be established or the config table doesn't exist
36          * - App::MODE_MAINTENANCE: The maintenance mode has been set
37          * - App::MODE_NORMAL     : Normal run with all features enabled
38          *
39          * @return Mode returns the determined mode
40          *
41          * @throws \Exception
42          */
43         public function determine(BasePath $basepath, Database $database, ConfigCache $configCache)
44         {
45                 $mode = 0;
46
47                 $basepathName = $basepath->getPath();
48
49                 if (!file_exists($basepathName . '/config/local.config.php')
50                     && !file_exists($basepathName . '/config/local.ini.php')
51                     && !file_exists($basepathName . '/.htconfig.php')) {
52                         return new Mode($mode);
53                 }
54
55                 $mode |= Mode::LOCALCONFIGPRESENT;
56
57                 if (!$database->connected()) {
58                         return new Mode($mode);
59                 }
60
61                 $mode |= Mode::DBAVAILABLE;
62
63                 if ($database->fetchFirst("SHOW TABLES LIKE 'config'") === false) {
64                         return new Mode($mode);
65                 }
66
67                 $mode |= Mode::DBCONFIGAVAILABLE;
68
69                 if (!empty($configCache->get('system', 'maintenance')) ||
70                     // Don't use Config or Configuration here because we're possibly BEFORE initializing the Configuration,
71                     // so this could lead to a dependency circle
72                     !empty($database->selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])['v'])) {
73                         return new Mode($mode);
74                 }
75
76                 $mode |= Mode::MAINTENANCEDISABLED;
77
78                 return new Mode($mode);
79         }
80
81         /**
82          * Checks, if the Friendica Node has the given mode
83          *
84          * @param int $mode A mode to test
85          *
86          * @return bool returns true, if the mode is set
87          */
88         public function has($mode)
89         {
90                 return ($this->mode & $mode) > 0;
91         }
92
93
94         /**
95          * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
96          *
97          * @return bool
98          */
99         public function isInstall()
100         {
101                 return !$this->has(Mode::LOCALCONFIGPRESENT) ||
102                        !$this->has(MODE::DBCONFIGAVAILABLE);
103         }
104
105         /**
106          * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
107          *
108          * @return bool
109          */
110         public function isNormal()
111         {
112                 return $this->has(Mode::LOCALCONFIGPRESENT) &&
113                        $this->has(Mode::DBAVAILABLE) &&
114                        $this->has(Mode::DBCONFIGAVAILABLE) &&
115                        $this->has(Mode::MAINTENANCEDISABLED);
116         }
117 }