1 | <?php |
||
19 | class Editor extends Model |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * Language model instance |
||
24 | * @var \gplcart\core\models\Language $language |
||
25 | */ |
||
26 | protected $language; |
||
27 | |||
28 | /** |
||
29 | * Backup model instance |
||
30 | * @var \gplcart\modules\backup\models\Backup $backup |
||
31 | */ |
||
32 | protected $backup; |
||
33 | |||
34 | /** |
||
35 | * @param LanguageModel $language |
||
36 | * @param ModuleBackupModel $backup |
||
37 | */ |
||
38 | public function __construct(LanguageModel $language, |
||
39 | ModuleBackupModel $backup) |
||
40 | { |
||
41 | parent::__construct(); |
||
42 | |||
43 | $this->backup = $backup; |
||
44 | $this->language = $language; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Returns an array of editable files |
||
49 | * @param array $module |
||
50 | * @return array |
||
51 | */ |
||
52 | public function getList(array $module) |
||
64 | |||
65 | /** |
||
66 | * Saves an edited file |
||
67 | * @param array $data |
||
68 | * @return boolean |
||
69 | */ |
||
70 | public function save($data) |
||
71 | { |
||
72 | $result = null; |
||
73 | $this->hook->attach('module.editor.save.before', $data, $result); |
||
74 | |||
75 | if (isset($result)) { |
||
76 | return $result; |
||
77 | } |
||
78 | |||
79 | $has_backup = true; |
||
80 | if (!$this->hasBackup($data['module'])) { |
||
81 | $has_backup = $this->backup->backup('module', $data['module']); |
||
82 | } |
||
83 | |||
84 | if ($has_backup !== true) { |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | $result = $this->write($data['content'], $data['path']); |
||
89 | |||
90 | $this->hook->attach('module.editor.save.after', $data, $result); |
||
91 | return $result; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Writes a content to a file |
||
96 | * @param string $content |
||
97 | * @param string $file |
||
98 | * @return boolean |
||
99 | */ |
||
100 | protected function write($content, $file) |
||
108 | |||
109 | /** |
||
110 | * Whether a module ID has a backup |
||
111 | * @param array $module |
||
112 | * @return boolean |
||
113 | */ |
||
114 | public function hasBackup(array $module) |
||
119 | |||
120 | /** |
||
121 | * Tries to validate syntax of a PHP file |
||
122 | * @param string $file |
||
123 | * @return mixed |
||
124 | */ |
||
125 | public function validatePhpFile($file) |
||
137 | |||
138 | /** |
||
139 | * Whether it's possible to validate a PHP code |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function canValidatePhpCode() |
||
146 | |||
147 | /** |
||
148 | * Tries to validate a PHP code |
||
149 | * @param string $code |
||
150 | * @return mixed |
||
151 | */ |
||
152 | public function validatePhpCode($code) |
||
162 | |||
163 | } |
||
164 |