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 "\nPlease run `composer install taeluf/cli v0.1` to use cli commands\n";
    exit;
}

$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){
        $_SERVER['REQUEST_URI'] = '/generic-error-page/';
        $_SERVER['HTTP_HOST'] = null;
        $_SERVER['REQUEST_METHOD'] = 'GET';

        $dir = getcwd();
        if (!is_file($dir.'/deliver.php'))$dir = $dir.'/test/Server/';

        ob_start();
        require($dir.'/deliver.php');
        ob_get_clean();

        $response = $lia->getResponse('/generic-error-page/')->content;

        // get address of stylesheet
        $reg = '/\<link rel="stylesheet" href="\/(lia\-resource\.[a-z0-9]+\.min\.css)" \/\>/';
        preg_match($reg, $response, $matches);

        // var_dump($matches[1]);
        // exit;
        $stylesheet = $lia->resources->cache->get_cache_file_content($matches[1]);

        $out = preg_replace($reg,"<style>\n$stylesheet</style>", $response);

        $cache_dir = $lia->cache->dir;
        if (!is_dir($cache_dir))mkdir($cache_dir);
        $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;
        $_SERVER['HTTP_HOST'] = NULL;
        $_SERVER['REQUEST_METHOD'] = 'GET';

        // error_reporting(1|2|4);
        echo "\nInitializing analytics table. Your deliver.php file MUST contain a var named \$lia.";

        $dir = getcwd();
        if (!is_file($dir.'/deliver.php'))$dir = $dir.'/test/Server/';

        try {
            require($dir.'/deliver.php');
        } catch (\Exception $e){
            //hopefully this is fine
        }

        if (!isset($lia->pdo)||!($lia->pdo instanceof \PDO)){
            echo "Your \$lia instance must have 'pdo' property on it.";
            return;
        }

        $lia->init_analytics_table($lia->pdo);

    }, "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();