Skip to content

Commit 7f9e3c7

Browse files
authored
Issue/symfony console removal close alexdebril#377 (alexdebril#382)
* `./bin/feedio` doesn't need Symfony anymore * Removed useless stuff * New Lint rules applied
1 parent 0c2c924 commit 7f9e3c7

File tree

14 files changed

+1854
-2609
lines changed

14 files changed

+1854
-2609
lines changed

bin/feedio

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,69 @@
11
#!/usr/bin/env php
22
<?php
33

4-
foreach( [__DIR__.'/../vendor/autoload.php', __DIR__.'/../../../autoload.php'] as $file ) {
5-
if ( file_exists($file) ) {
6-
require $file;
7-
}
8-
}
9-
10-
use Symfony\Component\Console\Application;
11-
12-
if ( class_exists('Symfony\Component\Console\Application') ) {
13-
$application = new Application();
14-
$application->setName('feed-io : the CLI feed reader');
4+
use FeedIo\FeedIo;
155

16-
$application->add(new \FeedIo\Command\CheckCommand());
17-
$application->add(new \FeedIo\Command\ReadCommand());
18-
$application->add(new \FeedIo\Command\DiscoverCommand());
19-
$application->run();
20-
} else {
21-
main($argc, $argv);
22-
exit;
6+
$file = __DIR__.'/../vendor/autoload.php';
7+
if ( file_exists($file) ) {
8+
require $file;
239
}
2410

25-
/**
26-
* This function is invoked if symfony/console is not installed
27-
*/
28-
function main($argc, $argv) {
29-
if ( $argc < 2 ) {
30-
echo 'feed-io version 2.4' . PHP_EOL;
11+
main($argc, $argv);
12+
13+
function main($argc, $argv)
14+
{
15+
if ( $argc < 3 || !in_array($argv[1], ['read', 'discover']) ) {
16+
echo 'feed-io: the CLI feed reader' . PHP_EOL;
3117
echo 'Usage : ' . PHP_EOL;
32-
echo "\t feed-io [url]" . PHP_EOL;
18+
echo "\t feedio read|discover [url]" . PHP_EOL;
3319
exit;
3420
}
3521

3622
$feedIo = \FeedIo\Factory::create()->getFeedIo();
37-
$feed = array_reverse(iterator_to_array($feedIo->read($argv[$argc-1])->getFeed()));
23+
switch($argv[1]) {
24+
case 'discover':
25+
discover($feedIo, $argv[2]);
26+
break;
27+
default:
28+
read($feedIo, $argv[2]);
29+
break;
30+
}
31+
}
32+
33+
function read(FeedIo $feedIo, string $url)
34+
{
35+
$result = $feedIo->read($url);
36+
$feed = $result->getFeed();
3837

39-
foreach( $feed as $i => $item ) {
40-
echo "\033[32m{$item->getLastModified()->format(\DateTime::ATOM)} : \033[34m{$item->getTitle()}\033[0m";
41-
echo strip_tags(nl2br($item->getDescription())) . PHP_EOL;
38+
$items = array_reverse(iterator_to_array($feed));
39+
foreach( $items as $i => $item ) {
40+
echo "\033[32m{$item->getLastModified()->format(\DateTime::ATOM)} - \033[34m{$item->getTitle()}\033[0m:" . PHP_EOL;
41+
echo "\t". strip_tags(nl2br($item->getContent())) . PHP_EOL;
42+
}
43+
44+
$nextUpdate = $result->getNextUpdate();
45+
echo "\033[32mNext time a new item may be published : \033[34m{$nextUpdate->format(\DATE_ATOM)}\033[0m" . PHP_EOL;
46+
47+
$updateStats = $result->getUpdateStats();
48+
49+
echo "\033[32mMinimum interval between items : \033[34m".formatDateInterval($updateStats->getMedianInterval())."\033[0m" . PHP_EOL;
50+
echo "\033[32mMedian interval : \033[34m".formatDateInterval($updateStats->getMedianInterval())."\033[0m" . PHP_EOL;
51+
echo "\033[32mAverage interval : \033[34m".formatDateInterval($updateStats->getAverageInterval())."\033[0m" . PHP_EOL;
52+
echo "\033[32mMaximum interval : \033[34m".formatDateInterval($updateStats->getMaxInterval())."\033[0m". PHP_EOL;
53+
}
54+
55+
function discover(FeedIo $feedIo, string $url)
56+
{
57+
$urls = $feedIo->discover($url);
58+
59+
foreach( $urls as $i => $url ) {
60+
echo "\033[32m{$i} : \033[34m{$url}\033[0m" . PHP_EOL;
4261
}
4362
}
63+
64+
function formatDateInterval(int $interval): string
65+
{
66+
$zero = new \DateTime('@0');
67+
$diff = $zero->diff(new \DateTime("@{$interval}"));
68+
return $diff->format('%a days, %h hours, %i minutes, %s seconds');
69+
}

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
"ext-json": "*",
2121
"ext-libxml": "*",
2222
"guzzlehttp/guzzle": "~6.2|~7.0",
23-
"psr/log": "~1.0",
24-
"symfony/console": "~3.4|~4.0|~5.0"
23+
"psr/log": "~1.0"
2524
},
2625
"require-dev": {
2726
"phpunit/phpunit": "~9.3.0",
2827
"monolog/monolog": "1.*",
29-
"friendsofphp/php-cs-fixer": "^2.4",
30-
"phpstan/phpstan": "^0.12.81"
28+
"phpstan/phpstan": "^0.12.81",
29+
"friendsofphp/php-cs-fixer": "^3.5"
3130
},
3231
"suggest": {
3332
"monolog/monolog": "Allows to handle logs"

0 commit comments

Comments
 (0)