|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Timegryd\OpcacheResetBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
9
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
10
|
|
|
use Timegryd\OpcacheResetBundle\Helper\OpcacheResetCommandHelper; |
|
11
|
|
|
|
|
12
|
|
|
class OpcacheResetCommand extends ContainerAwareCommand |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $defaultHost; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $defaultDir; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var OpcacheResetCommandHelper |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $helper; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($defaultHost, $defaultDir, OpcacheResetCommandHelper $opcacheResetCommandHelper, HttpClient $httpClient) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->defaultHost = $defaultHost; |
|
32
|
|
|
$this->defaultDir = $defaultDir; |
|
33
|
|
|
$this->helper = $opcacheResetCommandHelper; |
|
34
|
|
|
$this->httpClient = $httpClient; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
parent::__construct(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function configure() |
|
40
|
|
|
{ |
|
41
|
|
|
$this |
|
42
|
|
|
->setName('opcache:reset') |
|
43
|
|
|
->setDescription('Reset opcache') |
|
44
|
|
|
->addArgument('host', InputArgument::OPTIONAL, 'Base url to call', $this->defaultHost) |
|
45
|
|
|
->addArgument('dir', InputArgument::OPTIONAL, 'Web dir where to create file to call', $this->defaultDir) |
|
46
|
|
|
; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
50
|
|
|
{ |
|
51
|
|
|
$output->writeln(['Opcache reset begin', '==================='], OutputInterface::VERBOSITY_VERBOSE); |
|
52
|
|
|
|
|
53
|
|
|
$token = sha1(uniqid()); |
|
54
|
|
|
|
|
55
|
|
|
$this->helper->createFile($input->getArgument('dir'), $token); |
|
56
|
|
|
$output->writeln('File created', OutputInterface::VERBOSITY_VERBOSE); |
|
57
|
|
|
|
|
58
|
|
|
$url = $this->helper->generateUrl($input->getArgument('host'), $token); |
|
59
|
|
|
|
|
60
|
|
|
$response = $this->httpClient->request('GET', $url); |
|
61
|
|
|
|
|
62
|
|
|
$this->helper->clean($input->getArgument('dir')); |
|
63
|
|
|
$output->writeln('File deleted', OutputInterface::VERBOSITY_VERBOSE); |
|
64
|
|
|
|
|
65
|
|
|
$output->writeln($this->helper->handleResponse($response), OutputInterface::VERBOSITY_VERBOSE); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: