Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
StatusAbstract
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 activate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 activateRoutes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 activateHooks
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 installRoutesHooks
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 clearRoutes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clearHooks
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\System\File\PathException;
18use phpOMS\System\File\PermissionException;
19use phpOMS\Utils\Parser\Php\ArrayParser;
20
21/**
22 * Status abstract class.
23 *
24 * This abstraction can be used by modules in order to manipulate their basic status/state.
25 *
26 * @package phpOMS\Application
27 * @license OMS License 2.0
28 * @link    https://jingga.app
29 * @since   1.0.0
30 */
31abstract class StatusAbstract
32{
33    /**
34     * Path of the file
35     *
36     * @var string
37     * @since 1.0.0
38     */
39    public const PATH = '';
40
41    /**
42     * Deactivate app.
43     *
44     * @param ApplicationAbstract $app  Application
45     * @param ApplicationInfo     $info Module info
46     *
47     * @return void
48     *
49     * @since 1.0.0
50     */
51    public static function activate(ApplicationAbstract $app, ApplicationInfo $info) : void
52    {
53        self::activateRoutes($info);
54        self::activateHooks($info);
55    }
56
57    /**
58     * Init routes.
59     *
60     * @param ApplicationInfo $appInfo Application info
61     *
62     * @return void
63     *
64     * @since 1.0.0
65     */
66    public static function activateRoutes(ApplicationInfo $appInfo = null) : void
67    {
68        self::installRoutesHooks(static::PATH . '/../Routes.php', static::PATH . '/../Admin/Install/Application/Routes.php');
69    }
70
71    /**
72     * Init hooks.
73     *
74     * @param ApplicationInfo $appInfo Application info
75     *
76     * @return void
77     *
78     * @since 1.0.0
79     */
80    public static function activateHooks(ApplicationInfo $appInfo = null) : void
81    {
82        self::installRoutesHooks(static::PATH . '/../Hooks.php', static::PATH . '/../Admin/Install/Application/Hooks.php');
83    }
84
85    /**
86     * Install routes.
87     *
88     * @param string $destRoutePath Destination route path
89     * @param string $srcRoutePath  Source route path
90     *
91     * @return void
92     *
93     * @throws PathException
94     * @throws PermissionException
95     *
96     * @since 1.0.0
97     */
98    protected static function installRoutesHooks(string $destRoutePath, string $srcRoutePath) : void
99    {
100        if (!\is_file($srcRoutePath)) {
101            return;
102        }
103
104        if (!\is_file($destRoutePath)) {
105            \file_put_contents($destRoutePath, '<?php return [];');
106        }
107
108        if (!\is_file($destRoutePath)) {
109            throw new PathException($destRoutePath); // @codeCoverageIgnore
110        }
111
112        if (!\is_writable($destRoutePath)) {
113            throw new PermissionException($destRoutePath); // @codeCoverageIgnore
114        }
115
116        /** @noinspection PhpIncludeInspection */
117        $appRoutes = include $destRoutePath;
118        /** @noinspection PhpIncludeInspection */
119        $srcRoutes = include $srcRoutePath;
120
121        $appRoutes = \array_merge_recursive($appRoutes, $srcRoutes);
122
123        \file_put_contents($destRoutePath, '<?php return ' . ArrayParser::serializeArray($appRoutes) . ';', \LOCK_EX);
124    }
125
126    /**
127     * Clear all routes.
128     *
129     * @return void
130     *
131     * @since 1.0.0
132     */
133    public static function clearRoutes() : void
134    {
135        \file_put_contents(static::PATH . '/../Routes.php', '<?php return [];', \LOCK_EX);
136    }
137
138    /**
139     * Clear all hooks.
140     *
141     * @return void
142     *
143     * @since 1.0.0
144     */
145    public static function clearHooks() : void
146    {
147        \file_put_contents(static::PATH . '/../Hooks.php', '<?php return [];', \LOCK_EX);
148    }
149}