1 | <?php |
||
2 | |||
3 | namespace Apps\Model\Admin\Application; |
||
4 | |||
5 | use Apps\ActiveRecord\App as AppRecord; |
||
6 | use Ffcms\Core\Arch\Model; |
||
7 | use Ffcms\Core\Exception\SyntaxException; |
||
8 | use Ffcms\Core\Helper\Type\Arr; |
||
9 | use Ffcms\Core\Helper\Type\Str; |
||
10 | |||
11 | /** |
||
12 | * Class FormInstall. Install new app model |
||
13 | * @package Apps\Model\Admin\Application |
||
14 | */ |
||
15 | class FormInstall extends Model |
||
16 | { |
||
17 | public $sysname; |
||
18 | |||
19 | private $_apps; |
||
20 | private $_type; |
||
21 | private $_definedControllers = []; |
||
22 | |||
23 | /** |
||
24 | * FormInstall constructor. Pass applications object from controller |
||
25 | * @param $apps |
||
26 | * @param string $type |
||
27 | * @throws SyntaxException |
||
28 | */ |
||
29 | public function __construct(array $apps = null, $type) |
||
30 | { |
||
31 | $this->_apps = $apps; |
||
32 | // check if passed type is allowed to use |
||
33 | if (!Arr::in($type, ['app', 'widget'])) { |
||
34 | throw new SyntaxException('The type of extension is not defined!'); |
||
35 | } |
||
36 | $this->_type = $type; |
||
37 | parent::__construct(); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Insert applications defined controllers to local var |
||
42 | */ |
||
43 | public function before() |
||
44 | { |
||
45 | foreach ($this->_apps as $app) { |
||
46 | $this->_definedControllers[] = (string)$app->sys_name; |
||
47 | } |
||
48 | |||
49 | parent::before(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Label form |
||
54 | * @return array |
||
55 | */ |
||
56 | public function labels(): array |
||
57 | { |
||
58 | return [ |
||
59 | 'sysname' => __('System name') |
||
60 | ]; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Validation rules |
||
65 | * @return array |
||
66 | */ |
||
67 | public function rules(): array |
||
68 | { |
||
69 | return [ |
||
70 | ['sysname', 'required'], |
||
71 | ['sysname', 'notin', $this->_definedControllers] |
||
72 | ]; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Make app installation |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function make() |
||
80 | { |
||
81 | $cName = ucfirst(Str::lowerCase($this->sysname)); |
||
82 | $cPath = 'Apps\Controller\Admin\\' . $cName; |
||
83 | // if object class is not loaded - prevent install |
||
84 | if (!class_exists($cPath) || !defined($cPath . '::VERSION')) { |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | // get ext version |
||
89 | $cVersion = constant($cPath . '::VERSION'); |
||
90 | if ($cVersion === null || Str::likeEmpty($cVersion)) { |
||
91 | $cVersion = '1.0.0'; |
||
92 | } |
||
93 | |||
94 | // save row to db |
||
95 | $record = new AppRecord(); |
||
96 | $record->type = $this->_type; |
||
97 | $record->sys_name = $cName; |
||
98 | $record->name = ''; |
||
99 | $record->disabled = 1; |
||
100 | $record->version = $cVersion; |
||
101 | $record->save(); |
||
102 | |||
103 | // callback to install method in extension |
||
104 | if (method_exists($cPath, 'install')) { |
||
105 | call_user_func($cPath . '::install'); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
106 | } |
||
107 | |||
108 | return true; |
||
109 | } |
||
110 | } |
||
111 |