J. King
7 years ago
11 changed files with 246 additions and 58 deletions
@ -1,10 +1,19 @@ |
|||||
<?php |
<?php |
||||
namespace JKingWeb\Arsse; |
namespace JKingWeb\Arsse; |
||||
|
var_export(get_defined_constants()); |
||||
|
exit; |
||||
require_once __DIR__.DIRECTORY_SEPARATOR."bootstrap.php"; |
require_once __DIR__.DIRECTORY_SEPARATOR."bootstrap.php"; |
||||
Arsse::load(new Conf()); |
|
||||
|
|
||||
if(\PHP_SAPI=="cli") { |
if(\PHP_SAPI=="cli") { |
||||
(new Service)->watch(); |
// initialize the CLI; this automatically handles --help and --version |
||||
|
$cli = new CLI; |
||||
|
// load configuration |
||||
|
Arsse::load(new Conf()); |
||||
|
// handle CLI requests |
||||
|
$cli->dispatch(); |
||||
} else { |
} else { |
||||
(new REST)->dispatch(); |
// load configuration |
||||
|
Arsse::load(new Conf()); |
||||
|
// handle Web requests |
||||
|
(new REST)->dispatch()->output(); |
||||
} |
} |
@ -0,0 +1,53 @@ |
|||||
|
<?php |
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\Arsse; |
||||
|
|
||||
|
class CLI { |
||||
|
protected $args = []; |
||||
|
|
||||
|
protected function usage(): string { |
||||
|
$prog = basename($_SERVER['argv'][0]); |
||||
|
return <<<USAGE_TEXT |
||||
|
Usage: |
||||
|
$prog daemon |
||||
|
$prog feed refresh <n> |
||||
|
$prog --version |
||||
|
$prog --help | -h |
||||
|
|
||||
|
The Arsse command-line interface currently allows you to start the refresh |
||||
|
daemon or refresh a specific feed by numeric ID. |
||||
|
USAGE_TEXT; |
||||
|
} |
||||
|
|
||||
|
function __construct(array $argv = null) { |
||||
|
if(is_null($argv)) { |
||||
|
$argv = array_slice($_SERVER['argv'], 1); |
||||
|
} |
||||
|
$this->args = \Docopt::handle($this->usage(), [ |
||||
|
'argv' => $argv, |
||||
|
'help' => true, |
||||
|
'version' => VERSION, |
||||
|
]); |
||||
|
} |
||||
|
|
||||
|
function dispatch(array $args = null): int { |
||||
|
// act on command line |
||||
|
if(is_null($args)) { |
||||
|
$args = $this->args; |
||||
|
} |
||||
|
if($args['daemon']) { |
||||
|
return $this->daemon(); |
||||
|
} elseif($args['feed'] && $args['refresh']) { |
||||
|
return $this->feedRefresh((int) $args['<n>']); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected function daemon(bool $loop = true): int { |
||||
|
(new Service)->watch($loop); |
||||
|
return 0; // FIXME: should return the exception code of thrown exceptions |
||||
|
} |
||||
|
|
||||
|
protected function feedRefresh(int $id): int { |
||||
|
return (int) !Arsse::$db->feedUpdate($id); |
||||
|
} |
||||
|
} |
@ -0,0 +1,6 @@ |
|||||
|
<?php |
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\Arsse\REST; |
||||
|
|
||||
|
class Exception extends \JKingWeb\Arsse\AbstractException { |
||||
|
} |
@ -0,0 +1,43 @@ |
|||||
|
<?php |
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\Arsse\Service\Forking; |
||||
|
use JKingWeb\Arsse\Arsse; |
||||
|
|
||||
|
class Driver implements \JKingWeb\Arsse\Service\Driver { |
||||
|
protected $queue = []; |
||||
|
|
||||
|
static function driverName(): string { |
||||
|
return Arsse::$lang->msg("Driver.Service.Forking.Name"); |
||||
|
} |
||||
|
|
||||
|
static function requirementsMet(): bool { |
||||
|
return function_exists("popen"); |
||||
|
} |
||||
|
|
||||
|
function __construct() { |
||||
|
} |
||||
|
|
||||
|
function queue(int ...$feeds): int { |
||||
|
$this->queue = array_merge($this->queue, $feeds); |
||||
|
return sizeof($this->queue); |
||||
|
} |
||||
|
|
||||
|
function exec(): int { |
||||
|
$pp = []; |
||||
|
while($this->queue) { |
||||
|
$id = (int) array_shift($this->queue); |
||||
|
array_push($pp, popen('"'.\PHP_BINARY.'" "'.$_SERVER['argv'][0].'" feed refresh '.$id, "r")); |
||||
|
} |
||||
|
while($pp) { |
||||
|
$p = array_pop($pp); |
||||
|
fgets($p); // TODO: log output |
||||
|
pclose($p); |
||||
|
} |
||||
|
return Arsse::$conf->serviceQueueWidth - sizeof($this->queue); |
||||
|
} |
||||
|
|
||||
|
function clean(): bool { |
||||
|
$this->queue = []; |
||||
|
return true; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue