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+ }
0 commit comments