|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Loader; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Similar to parent, except that, given a folder, we scan all of its subfolders |
|
9
|
|
|
* |
|
10
|
|
|
* @todo allow to specify specific subfolders not to scan, eg: where the media files are stored |
|
11
|
|
|
*/ |
|
12
|
|
|
class FilesystemRecursive extends Filesystem |
|
13
|
|
|
{ |
|
14
|
|
|
protected function getDefinitions(array $paths = array(), $returnFilename = false) |
|
15
|
|
|
{ |
|
16
|
|
|
// if no paths defined, we look in all bundles |
|
17
|
|
View Code Duplication |
if (empty($paths)) { |
|
|
|
|
|
|
18
|
|
|
$paths = array(); |
|
19
|
|
|
/** @var $bundle \Symfony\Component\HttpKernel\Bundle\BundleInterface */ |
|
20
|
|
|
foreach($this->kernel->getBundles() as $bundle) |
|
21
|
|
|
{ |
|
22
|
|
|
$path = $bundle->getPath() . "/" . $this->versionDirectory; |
|
23
|
|
|
if (is_dir($path)) { |
|
24
|
|
|
$paths[] = $path; |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$definitions = array(); |
|
30
|
|
|
foreach($paths as $path) { |
|
31
|
|
|
if (is_file($path)) { |
|
32
|
|
|
$definitions[basename($path)] = $returnFilename ? $path : new MigrationDefinition( |
|
33
|
|
|
basename($path), |
|
34
|
|
|
$path, |
|
35
|
|
|
file_get_contents($path) |
|
36
|
|
|
); |
|
37
|
|
|
} elseif (is_dir($path)) { |
|
38
|
|
|
|
|
39
|
|
|
// seems like sorting of recursive dirs scan is though... |
|
40
|
|
|
|
|
41
|
|
|
$dirs = array(); |
|
42
|
|
|
$DI = new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO); |
|
43
|
|
|
foreach (new \RecursiveIteratorIterator($DI) as $file) { |
|
44
|
|
|
if ($file->isFile()) { |
|
45
|
|
|
$dirs[dirname($file->getRealpath())] = true; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
$dirs = array_keys($dirs); |
|
49
|
|
|
asort($dirs); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($dirs as $dir) { |
|
52
|
|
|
foreach (glob("$dir/*") as $filename) { |
|
53
|
|
|
if (is_file($filename)) { |
|
54
|
|
|
$definitions[basename($filename)] = |
|
55
|
|
|
$returnFilename ? realpath($filename) : new MigrationDefinition( |
|
56
|
|
|
basename($filename), |
|
57
|
|
|
realpath($filename), |
|
58
|
|
|
file_get_contents($filename) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
else { |
|
65
|
|
|
throw new \Exception("Path '$path' is neither a file nor directory"); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
ksort($definitions); |
|
69
|
|
|
|
|
70
|
|
|
return $definitions; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.