|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Extension\PageScanner; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
11
|
|
|
use Symfony\Component\Lock\LockFactory; |
|
12
|
|
|
use Symfony\Component\Lock\Store\FlockStore; |
|
13
|
|
|
|
|
14
|
|
|
class PageScannerCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
private $filesystem; |
|
17
|
|
|
private $scanner; |
|
18
|
|
|
private $pageClass; |
|
19
|
|
|
private $em; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct( |
|
22
|
|
|
PageScannerService $scanner, |
|
23
|
|
|
Filesystem $filesystem, |
|
24
|
|
|
EntityManagerInterface $em, |
|
25
|
|
|
string $pageClass, |
|
26
|
|
|
string $varDir |
|
27
|
|
|
) { |
|
28
|
|
|
parent::__construct(); |
|
29
|
|
|
$this->scanner = $scanner; |
|
30
|
|
|
$this->pageClass = $pageClass; |
|
31
|
|
|
$this->em = $em; |
|
32
|
|
|
$this->filesystem = $filesystem; |
|
33
|
|
|
PageScannerController::$fileCache = $varDir.PageScannerController::$fileCache; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function configure() |
|
37
|
|
|
{ |
|
38
|
|
|
$this |
|
39
|
|
|
->setName('page:scan') |
|
40
|
|
|
->addArgument('host', InputArgument::OPTIONAL); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function scanAllWithLock() |
|
44
|
|
|
{ |
|
45
|
|
|
$lock = (new LockFactory(new FlockStore()))->createLock('page-scan'); |
|
46
|
|
|
if ($lock->acquire()) { |
|
47
|
|
|
sleep(30); |
|
48
|
|
|
$errors = $this->scanAll(); |
|
49
|
|
|
$this->filesystem->dumpFile(PageScannerController::$fileCache, serialize($errors)); |
|
50
|
|
|
$lock->release(); |
|
51
|
|
|
|
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function scanAll() |
|
59
|
|
|
{ |
|
60
|
|
|
$pages = $this->em->getRepository($this->pageClass)->findAll(); |
|
61
|
|
|
|
|
62
|
|
|
$errors = []; |
|
63
|
|
|
$errorNbr = 0; |
|
64
|
|
|
|
|
65
|
|
|
foreach ($pages as $page) { |
|
66
|
|
|
// todo import scanner via setScanner + services.yaml |
|
67
|
|
|
$scan = $this->scanner->scan($page); |
|
68
|
|
|
if (true !== $scan) { |
|
69
|
|
|
$errors[$page->getId()] = $scan; |
|
70
|
|
|
$errorNbr = $errorNbr + \count($errors[$page->getId()]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($errorNbr > 500) { |
|
74
|
|
|
break; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $errors; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
82
|
|
|
{ |
|
83
|
|
|
//if ($input->getArgument('host')) |
|
84
|
|
|
|
|
85
|
|
|
if ($this->scanAllWithLock()) { |
|
86
|
|
|
$output->writeln('done...'); |
|
87
|
|
|
} else { |
|
88
|
|
|
$output->writeln('cannot acquire the lock...'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return 0; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|