#!/usr/bin/env php
<?php
$own_autoload = __DIR__.'/../vendor/autoload.php';
$from_bin_dir_autoload = dirname(__DIR__,3).'/autoload.php';
if (file_exists($from_bin_dir_autoload))require($from_bin_dir_autoload);
else require($own_autoload);
\Tlf\Server::$IS_TLFSERV_CLI = true;
// $cli->load_inputs(json_decode(file_get_contents(__DIR__.'/defaults.json'),true));
$cli = new \Tlf\Cli();
$cli->load_stdin();
$cli->load_command('main',
function($cli,$args){
$cli->call_command('help');
}, 'alias for help'
);
/**
* Run all the other commands
*/
$cli->load_command('all',
function($cli, $args){
echo "\n\nRunning all commands\n";
$cli->call_command('error-page');
$cli->call_command('generate-sitemap');
$cli->call_command('recompile-phad');
$cli->call_command('init-analytics');
echo "\n\nDone running all commands.\n\n";
}, "Run all the commands"
);
/**
* Generate an error page.
* There must be a file with the localhost port either at `.phptest-host` or `test/Server/.phptest-host`
* localhost server must also be running.
*
* Error page is written to `cache/generic-error-page.html`
*
*/
$cli->load_command('error-page',
function($cli, $args){
echo "\nLocalhost Port retrieved from .phptest-host file in current working directory.\n";
$host_file = getcwd().'/.phptest-host';
if (!is_file($host_file))$host_file = getcwd().'/test/Server/.phptest-host';
$port = file_get_contents($host_file);
$host = 'http://localhost:'.$port;
$response = file_get_contents($host.'/generic-error-page/');
// get address of stylesheet
$reg = '/\<link rel="stylesheet" href="(\/lia\-resource\.[a-z0-9]+\.min\.css)" \/\>/';
preg_match($reg, $response, $matches);
$stylesheet = file_get_contents($host.$matches[1]);
$out = preg_replace($reg,"<style>\n$stylesheet</style>", $response);
$cache_dir = getcwd().'/cache/';
// __DIR__.'/../cache/
if (!is_dir($cache_dir))mkdir($cache_dir);
// echo "\n\n\n-----------\n\n";
// echo $cache_dir.'/generic-error-page.html';
// echo "\n\n\n-----------\n\n";
// exit;
$success = file_put_contents($cache_dir.'/generic-error-page.html', $out);
if ($success){
echo "\nError page generated!";
} else {
echo "\nError page generation FAILED :(";
}
},
"Write error page to cache/generic-error-page.html by requesting /generic-error-page/ on localhost."
);
/**
* Generate sitemap for phad ONLY. Does not make sitemap for any other routes.
* your deliver.php script MUST define `$server` and call `$server->enable_phad()`
*/
$cli->load_command('generate-sitemap',
function($cli, $args){
$err_rep = error_reporting();
$_SERVER['REQUEST_URI'] = null;
error_reporting(1|2|4);
echo "\nGenerating Sitemap File. Your deliver.php file MUST contain a var named \$server.";
$dir = getcwd();
if (!is_file($dir.'/deliver.php'))$dir = $dir.'/test/Server/';
require($dir.'/deliver.php');
if (!isset($server->phad)){
echo "\nYou MUST call \$server->enable_phad() in your deliver.php script in order to generat sitemap.\n";
return;
}
$output_file = $server->phad->create_sitemap_file();
// error_reporting($err_rep);
$verify = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
if (strpos(file_get_contents($output_file), $verify)!==false){
echo "\nCreated sitemap file.";
} else {
echo "\nFailed to create sitemap file.";
}
}, "Generate sitemap of phad routes"
);
/**
* Recompile all phad views
* your deliver.php script MUST define `$server` and call `$server->enable_phad()`
*/
$cli->load_command('recompile-phad',
function($cli, $args){
$err_rep = error_reporting();
$_SERVER['REQUEST_URI'] = null;
error_reporting(1|2|4);
echo "\nRecompiling Phad Views. Your deliver.php file MUST contain a var named \$server.";
$dir = getcwd();
if (!is_file($dir.'/deliver.php'))$dir = $dir.'/test/Server/';
require($dir.'/deliver.php');
if (!isset($server->phad)){
echo "\nYou MUST call \$server->enable_phad() in your deliver.php script in order to recompile phad items.\n";
return;
}
$server->phad->compile_all_items();
echo "\nRecompiling phad items DONE (hopefully, there is no verification currently)";
}, "Recompile all phad views",
);
$cli->load_command('init-analytics',
function($cli, $args){
// $err_rep = error_reporting();
$_SERVER['REQUEST_URI'] = null;
// error_reporting(1|2|4);
echo "\nInitializing analytics table. Your deliver.php file MUST contain a var named \$pdo & one named \$server.";
$dir = getcwd();
if (!is_file($dir.'/deliver.php'))$dir = $dir.'/test/Server/';
require($dir.'/deliver.php');
if (!isset($pdo)||!($pdo instanceof \PDO)){
echo "You MUST set \$pdo var to a \\PDO object in your deliver.php script";
return;
}
$server->init_analytics_table($pdo);
echo "\nRecompiling phad items DONE (hopefully, there is no verification currently)";
}, "Create the analytics table in your database."
);
$cli->load_command('new',
function($cli, $args){
$new = $args['--'][0] ?? null;
if ($new!='server'){
echo "Only supports 'new server' currently.";
return;
}
if (!$cli->ask("Create Server in ".$cli->pwd))return;
$path = dirname(__DIR__).'/test/Server/';
$from_path = $path;
$files = scandir($path);
unset($files[0]);
unset($files[1]);
foreach ($files as $child){
if (is_dir($path.'/'.$child)&&$child!='.'&&$child!='..'){
$children = scandir($path.'/'.$child);
foreach ($children as $last){
if ($last=='.'||$last=='..') continue;
$files[] = $child.'/'.$last;
}
continue;
}
}
$to_path = $cli->pwd;
foreach ($files as $f){
$in_path = $from_path.'/'.$f;
$out_path = $to_path.'/'.$f;
// echo "\n$out_path\n";
if (is_file($in_path)){
if (is_file($out_path)&&(isset($args['all'])||!$cli->ask("Overwrite $f?")))continue;
else if (isset($args['ask'])&&!$cli->ask("Write ".$f))continue;
copy($in_path, $out_path);
} else if (is_dir($in_path)){
if (!is_dir($out_path)
&&(
!isset($args['ask'])
||$cli->ask("mkdir ".$f)
)
)mkdir($out_path);
}
}
}, "new server --all to overwrite all files without prompts or --ask to ask for every file copy"
);
$cli->execute();