lia
#!/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));
if (!class_exists('Tlf\\Cli')){
echo "\nRun `composer install taeluf/cli v0.1` to use cli commands\n";
exit;
}
$cli = new \Tlf\Cli();
$cli->exclude_from_help[] = 'main';
$cli->load_stdin();
$config_file = \Lia\Ext\Scrawl::get_config_file($cli->pwd);
if ($config_file == null){
echo "\n You must create a config file in one of the following locations (relative to current working directory): ".implode(", ", \Lia\Ext\Scrawl::$config_file_locations);
echo "\n\n";
echo "Sample Config JSON:\n\n";
$sample_json = file_get_contents(__DIR__.'/sample-config.json');
echo $sample_json;
echo "\n\n";
exit;
}
$cli->load_json_file($config_file);
$cli->load_command('main',
function($cli,$args){
$cli->call_command('help');
}, 'alias for help'
);
/**
* Generate an error page.
*
* There must be a file `.phptest-host` in the current working directory with a port number.
* The site must be running at http://localhost:PORT
* There must be a route for `/generic-error-page/`
*
* Error page is written to `cache/generic-error-page.html`
*
*/
$cli->load_command('error-page',
function(\Tlf\Cli $__cli, $__args){
$__route = $__cli->args['error_page_route'];
$__out_file = $__cli->pwd.'/'.$__cli->args['error_page_file'];
$__deliver_script = $__cli->pwd.'/'.$__cli->args['deliver_script'];
$__liaison_varname = $__cli->args['liaison_variable_name'];
$_SERVER['REQUEST_URI'] = $__route;
$_SERVER['HTTP_HOST'] = null;
$_SERVER['REQUEST_METHOD'] = 'GET';
$get_output = function($__deliver_script, $__liaison_varname, &$__liaison_instance): string {
ob_start();
require($__deliver_script);
$__liaison_instance = $$__liaison_varname;
return ob_get_clean();
};
$lia = null;
$output = $get_output($__deliver_script, $__liaison_varname, $lia);
if (!($lia instanceof \Lia)){
echo "\n\nCannot generate error page because Liaison instance is invalid\n\n";
exit;
}
$res = \Lia\Addon\Resources::from($lia);
$reg = '/\<link rel="stylesheet" href="\/(lia\-resource\.[a-z0-9]+\.min\.css)" \/\>/';
preg_match($reg, $output, $matches);
$stylesheet = $res->cache->get_cache_file_content($matches[1]);
$output = preg_replace($reg,"<style>\n$stylesheet</style>", $output);
$reg = '/\<script type="text\/javascript" src="\/(lia\-resource\.[a-z0-9]+\.min\.js)"><\/script>/';
preg_match($reg, $output, $matches);
$scripts = $res->cache->get_cache_file_content($matches[1]);
$output = preg_replace($reg,"<script type=\"text/javascript\">\n$scripts\n</script>", $output);
$success = file_put_contents($__out_file, $output);
if ($success){
echo "\nSUCCESS: Error page generated at $__out_file!";
echo "\n\nNOTE1: If stylesheet or script output is incorrect, try deleting the cache directory at '".$res->cache->dir."'\n\n";
echo "NOTE2: Only compiled scripts and stylesheets are output directly. Any css/js added via URLs will remain untouched, which could cause your error page to fail.\n\n";
} else {
echo "\nFAILURE: Generated error page failed to write to disk at '$__out_file'";
}
},
"Write an HTML error page to disk, with compiled css & js printed inline."
);
$cli->load_command('clear-cache',
function(\Tlf\Cli $cli, $args){
$deliver_script = $cli->pwd.'/'.$cli->args['deliver_script'];
$liaison_varname = $cli->args['liaison_variable_name'];
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['HTTP_HOST'] = null;
$_SERVER['REQUEST_METHOD'] = 'GET';
$get_output = function($__deliver_script, $__liaison_varname, &$__liaison_instance): string {
ob_start();
require($__deliver_script);
$__liaison_instance = $$__liaison_varname;
return ob_get_clean();
};
$lia = null;
$output = $get_output($deliver_script, $liaison_varname, $lia);
if (!($lia instanceof \Lia)){
echo "\n\nCannot clear cache because configured deliver script and/or liaison variable name are invalid.\n\n";
exit;
}
if (!isset($args['stale']) || $args['stale'] != true){
echo "\n\nDelete ALL cached files\n\n";
\Lia\Addon\Resources::from($lia)->cache->delete_all_cache_files();
\Lia\Addon\Cache::from($lia)->delete_all_cache_files();
} else {
echo "\n\nDelete EXPIRED cached files\n\n";
\Lia\Addon\Resources::from($lia)->cache->delete_stale_files();
\Lia\Addon\Cache::from($lia)->delete_stale_files();
}
},
"Delete the main cache and the resource files cache. Pass '--stale' to only delete expired files."
);
$cli->load_command('dump',
function(\Tlf\Cli $cli, $args){
$deliver_script = $cli->pwd.'/'.$cli->args['deliver_script'];
$liaison_varname = $cli->args['liaison_variable_name'];
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['HTTP_HOST'] = null;
$_SERVER['REQUEST_METHOD'] = 'GET';
$get_output = function($__deliver_script, $__liaison_varname, &$__liaison_instance): string {
ob_start();
require($__deliver_script);
$__liaison_instance = $$__liaison_varname;
return ob_get_clean();
};
$lia = null;
$output = $get_output($deliver_script, $liaison_varname, $lia);
if (!($lia instanceof \Lia)){
echo "\n\nCannot dump liaison information because configured deliver script and/or liaison variable name are invalid.\n\n";
exit;
}
$lia->dump();
},
"Print information about your Lia instance, including routes, packages and addon lists, views, and more."
);
$cli->execute();