1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ExpressApi\V1\Rpc\SetMode; |
4
|
|
|
|
5
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
6
|
|
|
use ZF\ApiProblem\ApiProblem; |
7
|
|
|
use ZF\ApiProblem\ApiProblemResponse; |
8
|
|
|
|
9
|
|
|
class SetModeController extends AbstractActionController { |
10
|
|
|
|
11
|
|
|
private $htaccessName = '.htaccess'; |
12
|
|
|
private $modesWhiteList = array( |
13
|
|
|
'production', |
14
|
|
|
'development' |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
public function setModeAction() { |
18
|
|
|
$response = array( |
19
|
|
|
'success' => false |
20
|
|
|
); |
21
|
|
|
$modeId = $this->params()->fromRoute('mode_id'); |
22
|
|
|
if(in_array($modeId, $this->modesWhiteList)) { |
23
|
|
|
$htaccess = $this->getPublicPath().$this->htaccessName; |
24
|
|
|
if(file_exists($htaccess) && is_file($htaccess)) { |
25
|
|
|
$htaccessContent = $this->changeHtaccessMode($this->loadHtaccess($htaccess), $modeId); |
26
|
|
|
$response['success'] = $this->writeHtaccess($htaccess, $htaccessContent); |
27
|
|
|
} |
28
|
|
|
else { |
29
|
|
|
return new ApiProblemResponse(new ApiProblem(400, 'Htaccess file not exist.')); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
else { |
33
|
|
|
return new ApiProblemResponse(new ApiProblem(400, 'The input mode is not allowed.')); |
34
|
|
|
} |
35
|
|
|
return $response; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function getPublicPath() { |
39
|
|
|
$config = $this->getServiceLocator()->get('Config'); |
40
|
|
|
if(is_array($config) && |
41
|
|
|
isset($config['template_assets_resolver']) && |
42
|
|
|
isset($config['template_assets_resolver']['public_path'])) { |
43
|
|
|
return $config['template_assets_resolver']['public_path']; |
44
|
|
|
} |
45
|
|
|
return ''; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function loadHtaccess($path) { |
49
|
|
|
return file_get_contents($path); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function writeHtaccess($path, $content) { |
53
|
|
|
return (file_put_contents($path, $content) > 0); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function changeHtaccessMode($content, $mode) { |
57
|
|
|
$modeLine = 'SetEnv APP_ENV '; |
58
|
|
|
foreach($this->modesWhiteList as $wMode) { |
59
|
|
|
$content = str_replace($modeLine.$wMode, $modeLine.$mode, $content); |
60
|
|
|
} |
61
|
|
|
return $content; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|