1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bex\Behat\ScreenshotExtension\Driver; |
4
|
|
|
|
5
|
|
|
use Bex\Behat\ScreenshotExtension\Driver\ImageDriverInterface; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
10
|
|
|
use Symfony\Component\Finder\Finder; |
11
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
12
|
|
|
|
13
|
|
|
class Local implements ImageDriverInterface |
14
|
|
|
{ |
15
|
|
|
const DEFAULT_DIRECTORY = 'behat-screenshot'; |
16
|
|
|
const CONFIG_PARAM_SCREENSHOT_DIRECTORY = 'screenshot_directory'; |
17
|
|
|
const CONFIG_PARAM_CLEAR_SCREENSHOT_DIRECTORY = 'clear_screenshot_directory'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Filesystem |
21
|
|
|
*/ |
22
|
|
|
private $filesystem; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $screenshotDirectory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Finder |
31
|
|
|
*/ |
32
|
|
|
private $finder; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \finfo |
36
|
|
|
*/ |
37
|
|
|
private $fileInfo; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Filesystem $filesystem |
41
|
|
|
* @param Finder $finder |
42
|
|
|
* @param \finfo $fileInfo |
43
|
|
|
*/ |
44
|
|
|
public function __construct(Filesystem $filesystem = null, Finder $finder = null, \finfo $fileInfo = null) |
45
|
|
|
{ |
46
|
|
|
$this->filesystem = $filesystem ?: new Filesystem(); |
47
|
|
|
$this->finder = $finder ?: new Finder(); |
48
|
|
|
$this->fileInfo = $fileInfo ?: new \finfo(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ArrayNodeDefinition $builder |
53
|
|
|
*/ |
54
|
|
|
public function configure(ArrayNodeDefinition $builder) |
55
|
|
|
{ |
56
|
|
|
$builder |
57
|
|
|
->children() |
58
|
|
|
->scalarNode(self::CONFIG_PARAM_SCREENSHOT_DIRECTORY) |
59
|
|
|
->defaultValue($this->getDefaultDirectory()) |
60
|
|
|
->end() |
61
|
|
|
->booleanNode(self::CONFIG_PARAM_CLEAR_SCREENSHOT_DIRECTORY) |
62
|
|
|
->defaultValue(false) |
63
|
|
|
->end() |
64
|
|
|
->end(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param ContainerBuilder $container |
69
|
|
|
* @param array $config |
70
|
|
|
*/ |
71
|
|
|
public function load(ContainerBuilder $container, array $config) |
72
|
|
|
{ |
73
|
|
|
$this->screenshotDirectory = $config[self::CONFIG_PARAM_SCREENSHOT_DIRECTORY]; |
74
|
|
|
|
75
|
|
|
if ($config[self::CONFIG_PARAM_CLEAR_SCREENSHOT_DIRECTORY]) { |
76
|
|
|
$this->clearScreenshotDirectory(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $binaryImage |
82
|
|
|
* @param string $filename |
83
|
|
|
* |
84
|
|
|
* @return string URL to the image |
85
|
|
|
*/ |
86
|
|
|
public function upload($binaryImage, $filename) |
87
|
|
|
{ |
88
|
|
|
$targetFile = $this->getTargetPath($filename); |
89
|
|
|
$this->ensureDirectoryExists(dirname($targetFile)); |
90
|
|
|
$this->filesystem->dumpFile($targetFile, $binaryImage); |
91
|
|
|
|
92
|
|
|
return $targetFile; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function getDefaultDirectory() |
99
|
|
|
{ |
100
|
|
|
return sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::DEFAULT_DIRECTORY; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $fileName |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
private function getTargetPath($fileName) |
109
|
|
|
{ |
110
|
|
|
$path = rtrim($this->screenshotDirectory, DIRECTORY_SEPARATOR); |
111
|
|
|
return empty($path) ? $fileName : $path . DIRECTORY_SEPARATOR . $fileName; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $directory |
116
|
|
|
* |
117
|
|
|
* @throws IOException |
118
|
|
|
*/ |
119
|
|
|
private function ensureDirectoryExists($directory) |
120
|
|
|
{ |
121
|
|
|
try { |
122
|
|
|
if (!$this->filesystem->exists($directory)) { |
123
|
|
|
$this->filesystem->mkdir($directory, 0770); |
124
|
|
|
} |
125
|
|
|
} catch (IOException $e) { |
126
|
|
|
throw new \RuntimeException(sprintf('Cannot create screenshot directory "%s".', $directory)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function clearScreenshotDirectory() |
131
|
|
|
{ |
132
|
|
|
$filesToDelete = []; |
133
|
|
|
|
134
|
|
|
/** @var SplFileInfo $file */ |
135
|
|
|
foreach ($this->finder->files()->in($this->getTargetPath('')) as $file) { |
136
|
|
|
if (strpos($this->fileInfo->file($file->getRealPath(), FILEINFO_MIME_TYPE), 'image/') !== false) { |
137
|
|
|
$filesToDelete[] = $file->getRealPath(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->filesystem->remove($filesToDelete); |
142
|
|
|
} |
143
|
|
|
} |