Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.00% covered (success)
95.00%
38 / 40
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
InstallerAbstract
95.00% covered (success)
95.00%
38 / 40
80.00% covered (warning)
80.00%
4 / 5
20
0.00% covered (danger)
0.00%
0 / 1
 install
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 installTheme
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
8
 createTables
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
5.20
 activate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 reInit
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Application
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Application;
16
17use phpOMS\Autoloader;
18use phpOMS\Config\SettingsInterface;
19use phpOMS\DataStorage\Database\DatabasePool;
20use phpOMS\DataStorage\Database\Schema\Builder as SchemaBuilder;
21use phpOMS\System\File\Local\Directory;
22
23/**
24 * Installer abstract class.
25 *
26 * @package phpOMS\Application
27 * @license OMS License 2.0
28 * @link    https://jingga.app
29 * @since   1.0.0
30 */
31abstract class InstallerAbstract
32{
33    /**
34     * Path of the file
35     *
36     * @var string
37     * @since 1.0.0
38     */
39    public const PATH = '';
40
41    /**
42     * Install app.
43     *
44     * @param ApplicationAbstract $app        Application
45     * @param ApplicationInfo     $info       App info
46     * @param SettingsInterface   $cfgHandler Settings/Configuration handler
47     *
48     * @return void
49     *
50     * @since 1.0.0
51     */
52    public static function install(ApplicationAbstract $app, ApplicationInfo $info, SettingsInterface $cfgHandler) : void
53    {
54        self::createTables($app->dbPool, $info);
55        self::activate($app, $info);
56        self::installTheme(static::PATH . '/..', 'Default');
57    }
58
59    /**
60     * Install the theme
61     *
62     * @param string $destination Destination of the application
63     * @param string $theme       Theme name
64     *
65     * @return void
66     *
67     * @since 1.0.0
68     */
69    public static function installTheme(string $destination, string $theme) : void
70    {
71        if (!\is_dir($path = $destination . '/Themes/' . $theme)) {
72            return;
73        }
74
75        $dirs = \scandir($path);
76        if ($dirs === false) {
77            return; // @codeCoverageIgnore
78        }
79
80        foreach ($dirs as $dir) {
81            if (!\is_dir($path. '/' . $dir) || $dir === '.' || $dir === '..') {
82                continue;
83            }
84
85            if (\is_dir($destination . '/' . $dir)) {
86                Directory::delete($destination . '/' . $dir);
87            }
88
89            Directory::copy(
90                $destination . '/Themes/' . $theme . '/' . $dir,
91                $destination . '/' . $dir,
92                true
93            );
94        }
95    }
96
97    /**
98     * Create tables for app.
99     *
100     * @param DatabasePool    $dbPool Database instance
101     * @param ApplicationInfo $info   App info
102     *
103     * @return void
104     *
105     * @since 1.0.0
106     */
107    protected static function createTables(DatabasePool $dbPool, ApplicationInfo $info) : void
108    {
109        $path = static::PATH . '/Install/db.json';
110        if (!\is_file($path)) {
111            return;
112        }
113
114        $content = \file_get_contents($path);
115        if ($content === false) {
116            return; // @codeCoverageIgnore
117        }
118
119        $definitions = \json_decode($content, true);
120        if (!\is_array($definitions)) {
121            return;
122        }
123
124        foreach ($definitions as $definition) {
125            SchemaBuilder::createFromSchema($definition, $dbPool->get('schema'))->execute();
126        }
127    }
128
129    /**
130     * Activate after install.
131     *
132     * @param ApplicationAbstract $app  Application
133     * @param ApplicationInfo     $info App info
134     *
135     * @return void
136     *
137     * @throws \UnexpectedValueException
138     *
139     * @since 1.0.0
140     */
141    protected static function activate(ApplicationAbstract $app, ApplicationInfo $info) : void
142    {
143        if (($path = \realpath(static::PATH)) === false) {
144            return; // @codeCoverageIgnore
145        }
146
147        $classPath = \substr($path . '/Status', (int) \strlen((string) \realpath(__DIR__ . '/../../')));
148
149        // @var class-string<StatusAbstract> $class
150        $class = \strtr($classPath, '/', '\\');
151
152        if (!Autoloader::exists($class)) {
153            throw new \UnexpectedValueException($class); // @codeCoverageIgnore
154        }
155
156        $class::activate($app, $info);
157    }
158
159    /**
160     * Re-init app.
161     *
162     * @param ApplicationInfo $info App info
163     *
164     * @return void
165     *
166     * @throws \UnexpectedValueException
167     *
168     * @since 1.0.0
169     */
170    public static function reInit(ApplicationInfo $info) : void
171    {
172        if (($path = \realpath(static::PATH)) === false) {
173            return; // @codeCoverageIgnore
174        }
175
176        $classPath = \substr($path . '/Status', (int) \strlen((string) \realpath(__DIR__ . '/../../')));
177
178        // @var class-string<StatusAbstract> $class
179        $class = \strtr($classPath, '/', '\\');
180
181        if (!Autoloader::exists($class)) {
182            throw new \UnexpectedValueException($class); // @codeCoverageIgnore
183        }
184
185        $class::clearRoutes();
186        $class::clearHooks();
187
188        $class::activateRoutes($info);
189        $class::activateHooks($info);
190    }
191}