1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace AnimeDb\Bundle\CatalogBundle\Service\Storage; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
13
|
|
|
use AnimeDb\Bundle\AppBundle\Service\CommandExecutor; |
14
|
|
|
use AnimeDb\Bundle\CatalogBundle\Entity\Storage as StorageEntity; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Storage scanner service. |
18
|
|
|
* |
19
|
|
|
* @author Peter Gribanov <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ScanExecutor |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var CommandExecutor |
25
|
|
|
*/ |
26
|
|
|
protected $command; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Filesystem |
30
|
|
|
*/ |
31
|
|
|
protected $fs; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $output = ''; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $progress = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param CommandExecutor $command |
45
|
|
|
* @param Filesystem $fs |
46
|
|
|
* @param string $output |
47
|
|
|
* @param string $progress |
48
|
|
|
*/ |
49
|
1 |
|
public function __construct(CommandExecutor $command, Filesystem $fs, $output, $progress) |
50
|
|
|
{ |
51
|
1 |
|
$this->progress = $progress; |
52
|
1 |
|
$this->command = $command; |
53
|
1 |
|
$this->output = $output; |
54
|
1 |
|
$this->fs = $fs; |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Scan storage in background and export progress and output. |
59
|
|
|
* |
60
|
|
|
* @param StorageEntity $storage |
61
|
|
|
*/ |
62
|
1 |
|
public function export(StorageEntity $storage) |
63
|
|
|
{ |
64
|
1 |
|
$output = sprintf($this->output, $storage->getId()); |
65
|
1 |
|
$progress = sprintf($this->progress, $storage->getId()); |
66
|
|
|
|
67
|
1 |
|
$this->fs->mkdir([dirname($output), dirname($progress)], 0755); |
68
|
1 |
|
$this->fs->remove([$output, $progress]); |
69
|
1 |
|
$this->fs->touch($output); |
70
|
1 |
|
file_put_contents($progress, '0%'); |
71
|
|
|
|
72
|
|
|
$this->command->send(sprintf( |
73
|
|
|
'php app/console animedb:scan-storage --no-ansi --force --export=%s %s >%s 2>&1', |
74
|
|
|
$progress, |
75
|
|
|
$storage->getId(), |
76
|
|
|
$output |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param StorageEntity $storage |
82
|
|
|
*/ |
83
|
|
|
public function forceStopScan(StorageEntity $storage) |
84
|
|
|
{ |
85
|
|
|
file_put_contents(sprintf($this->progress, $storage->getId()), '100%'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|