|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\LearnYouPhp\Exercise; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
|
6
|
|
|
use PhpSchool\PhpWorkshop\Exercise\CliExercise; |
|
7
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
|
8
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
|
9
|
|
|
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait; |
|
10
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck; |
|
11
|
|
|
use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
|
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class FilteredLs |
|
16
|
|
|
* @package PhpSchool\LearnYouPhp\Exercise |
|
17
|
|
|
* @author Aydin Hassan <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class FilteredLs extends AbstractExercise implements ExerciseInterface, CliExercise |
|
20
|
|
|
{ |
|
21
|
|
|
use TemporaryDirectoryTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Filesystem |
|
25
|
|
|
*/ |
|
26
|
|
|
private $filesystem; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Filesystem $filesystem |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Filesystem $filesystem) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->filesystem = $filesystem; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getName() |
|
40
|
|
|
{ |
|
41
|
|
|
return 'Filtered LS'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getDescription() |
|
48
|
|
|
{ |
|
49
|
|
|
return 'Read files in a folder and filter by a given extension'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getArgs() |
|
56
|
|
|
{ |
|
57
|
|
|
$folder = $this->getTemporaryPath(); |
|
58
|
|
|
|
|
59
|
|
|
$files = [ |
|
60
|
|
|
"learnyouphp.dat", |
|
61
|
|
|
"learnyouphp.txt", |
|
62
|
|
|
"learnyouphp.sql", |
|
63
|
|
|
"txt", |
|
64
|
|
|
"sql", |
|
65
|
|
|
"api.html", |
|
66
|
|
|
"html", |
|
67
|
|
|
"README.md", |
|
68
|
|
|
"CHANGELOG.md", |
|
69
|
|
|
"LICENCE.md", |
|
70
|
|
|
"md", |
|
71
|
|
|
"data.json", |
|
72
|
|
|
"json", |
|
73
|
|
|
"data.dat", |
|
74
|
|
|
"words.dat", |
|
75
|
|
|
"w00t.dat", |
|
76
|
|
|
"w00t.txt", |
|
77
|
|
|
"wrrrrongdat", |
|
78
|
|
|
"dat", |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
$this->filesystem->mkdir($folder); |
|
82
|
|
|
array_walk($files, function ($file) use ($folder) { |
|
83
|
|
|
$this->filesystem->dumpFile(sprintf('%s/%s', $folder, $file), ''); |
|
84
|
|
|
}); |
|
85
|
|
|
|
|
86
|
|
|
$ext = ''; |
|
87
|
|
|
while ($ext === '') { |
|
88
|
|
|
$index = array_rand($files); |
|
89
|
|
|
$ext = pathinfo($files[$index], PATHINFO_EXTENSION); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return [$folder, $ext]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return null |
|
97
|
|
|
*/ |
|
98
|
|
|
public function tearDown() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->filesystem->remove($this->getTemporaryPath()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return ExerciseType |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getType() |
|
107
|
|
|
{ |
|
108
|
|
|
return ExerciseType::CLI(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|