-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.php
41 lines (39 loc) · 1.44 KB
/
script.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
require_once __DIR__ . '/vendor/autoload.php';
use IponwebTest\LinesCounter\LinesCounter;
use IponwebTest\LineFilter\RTrimFilter\RTrimFilter;
use IponwebTest\FileLinesReader\FileLinesCounter;
use IponwebTest\FileLinesReader\Exception\FileException;
use IponwebTest\LinesCounter\Exception\NumException;
try {
if (!isset($_SERVER['argv'][1])|| !isset($_SERVER['argv'][2])) {
throw new \Exception('Argument error! Format is: php ./script.php filename num');
}
$fileName = $_SERVER['argv'][1];
$num = intval($_SERVER['argv'][2]);
$linesCounter = new LinesCounter();
$lineFilter = new RTrimFilter();
$fileLinesCounter = new FileLinesCounter($fileName, $linesCounter, $lineFilter);
$fileLinesCounter->countLines();
$result = $fileLinesCounter->getTopLines($num);
$output = '';
foreach ($result as $line => $num)
{
$output .= $line . ': ' . $num . "\n";
}
$std = fopen('php://stdout', 'w');
fwrite($std, $output);
fclose($std);
} catch (FileException $exception) {
$std = fopen('php://stderr', 'w');
fwrite($std, 'ERROR: ' . $exception->getMessage() . "\n");
fclose($std);
} catch (NumException $exception) {
$std = fopen('php://stderr', 'w');
fwrite($std, 'ERROR: ' . $exception->getMessage() . "\n");
fclose($std);
} catch (\Exception $exception) {
$std = fopen('php://stderr', 'w');
fwrite($std, 'ERROR: ' . $exception->getMessage() . "\n");
fclose($std);
}