Skip to content

Commit

Permalink
Issue/symfony console removal close #377 (#382)
Browse files Browse the repository at this point in the history
* `./bin/feedio` doesn't need Symfony anymore

* Removed useless stuff

* New Lint rules applied
  • Loading branch information
alexdebril authored Feb 1, 2022
1 parent 0c2c924 commit 7f9e3c7
Show file tree
Hide file tree
Showing 14 changed files with 1,854 additions and 2,609 deletions.
84 changes: 55 additions & 29 deletions bin/feedio
Original file line number Diff line number Diff line change
@@ -1,43 +1,69 @@
#!/usr/bin/env php
<?php

foreach( [__DIR__.'/../vendor/autoload.php', __DIR__.'/../../../autoload.php'] as $file ) {
if ( file_exists($file) ) {
require $file;
}
}

use Symfony\Component\Console\Application;

if ( class_exists('Symfony\Component\Console\Application') ) {
$application = new Application();
$application->setName('feed-io : the CLI feed reader');
use FeedIo\FeedIo;

$application->add(new \FeedIo\Command\CheckCommand());
$application->add(new \FeedIo\Command\ReadCommand());
$application->add(new \FeedIo\Command\DiscoverCommand());
$application->run();
} else {
main($argc, $argv);
exit;
$file = __DIR__.'/../vendor/autoload.php';
if ( file_exists($file) ) {
require $file;
}

/**
* This function is invoked if symfony/console is not installed
*/
function main($argc, $argv) {
if ( $argc < 2 ) {
echo 'feed-io version 2.4' . PHP_EOL;
main($argc, $argv);

function main($argc, $argv)
{
if ( $argc < 3 || !in_array($argv[1], ['read', 'discover']) ) {
echo 'feed-io: the CLI feed reader' . PHP_EOL;
echo 'Usage : ' . PHP_EOL;
echo "\t feed-io [url]" . PHP_EOL;
echo "\t feedio read|discover [url]" . PHP_EOL;
exit;
}

$feedIo = \FeedIo\Factory::create()->getFeedIo();
$feed = array_reverse(iterator_to_array($feedIo->read($argv[$argc-1])->getFeed()));
switch($argv[1]) {
case 'discover':
discover($feedIo, $argv[2]);
break;
default:
read($feedIo, $argv[2]);
break;
}
}

function read(FeedIo $feedIo, string $url)
{
$result = $feedIo->read($url);
$feed = $result->getFeed();

foreach( $feed as $i => $item ) {
echo "\033[32m{$item->getLastModified()->format(\DateTime::ATOM)} : \033[34m{$item->getTitle()}\033[0m";
echo strip_tags(nl2br($item->getDescription())) . PHP_EOL;
$items = array_reverse(iterator_to_array($feed));
foreach( $items as $i => $item ) {
echo "\033[32m{$item->getLastModified()->format(\DateTime::ATOM)} - \033[34m{$item->getTitle()}\033[0m:" . PHP_EOL;
echo "\t". strip_tags(nl2br($item->getContent())) . PHP_EOL;
}

$nextUpdate = $result->getNextUpdate();
echo "\033[32mNext time a new item may be published : \033[34m{$nextUpdate->format(\DATE_ATOM)}\033[0m" . PHP_EOL;

$updateStats = $result->getUpdateStats();

echo "\033[32mMinimum interval between items : \033[34m".formatDateInterval($updateStats->getMedianInterval())."\033[0m" . PHP_EOL;
echo "\033[32mMedian interval : \033[34m".formatDateInterval($updateStats->getMedianInterval())."\033[0m" . PHP_EOL;
echo "\033[32mAverage interval : \033[34m".formatDateInterval($updateStats->getAverageInterval())."\033[0m" . PHP_EOL;
echo "\033[32mMaximum interval : \033[34m".formatDateInterval($updateStats->getMaxInterval())."\033[0m". PHP_EOL;
}

function discover(FeedIo $feedIo, string $url)
{
$urls = $feedIo->discover($url);

foreach( $urls as $i => $url ) {
echo "\033[32m{$i} : \033[34m{$url}\033[0m" . PHP_EOL;
}
}

function formatDateInterval(int $interval): string
{
$zero = new \DateTime('@0');
$diff = $zero->diff(new \DateTime("@{$interval}"));
return $diff->format('%a days, %h hours, %i minutes, %s seconds');
}
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
"ext-json": "*",
"ext-libxml": "*",
"guzzlehttp/guzzle": "~6.2|~7.0",
"psr/log": "~1.0",
"symfony/console": "~3.4|~4.0|~5.0"
"psr/log": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~9.3.0",
"monolog/monolog": "1.*",
"friendsofphp/php-cs-fixer": "^2.4",
"phpstan/phpstan": "^0.12.81"
"phpstan/phpstan": "^0.12.81",
"friendsofphp/php-cs-fixer": "^3.5"
},
"suggest": {
"monolog/monolog": "Allows to handle logs"
Expand Down
Loading

0 comments on commit 7f9e3c7

Please sign in to comment.