<?php

declare(strict_types=1);

/** 
 * @author Robert Strutts <Robert@TryingToScale.com>
 * @copyright Copyright (c) 2022, Robert Strutts.
 * @license https://mit-license.org/
 */

namespace prj\mockup\cli_controllers\app;

class home_ctrl {
    
  public function index() {
      echo "Welcome to Index CLI...";
  }
  
  public function op_cache() {
      if (!opcache_get_status()) {
          echo "Op-Cache NOT Enabled!!! Must be run only by PHP_FPM not CLI Directly..." . PHP_EOL;
          echo "For PHP-FPM add to php.ini: opcache.enable=1" . PHP_EOL;
          echo "And: opcache.preload=/var/www/tts_project/op_cache.php" . PHP_EOL;
          exit(1);
      }
      
      $all_files = [];      
      
      $prj = PROJECT_BASE_DIR;
      
      $public = dirname($prj, 1) . "/public/";
      
      $all_files[] = $public . "main.page";
      
      $all_files[] = \main_tts\TTS_FRAMEWORK . "main.inc.php";
            
      $dir = \main_tts\TTS_FRAMEWORK . "bootstrap/";
      foreach (glob($dir . "*.php") as $filename) {
          if (str_contains($filename, "errors.php")) {
              continue;
          }
          $all_files[] = $filename;
      }      

      $dir = \main_tts\TTS_FRAMEWORK . "classes/";
      $all_files[] = $dir . "console_app.php";
      $all_files[] = $dir . "app.php";
      $all_files[] = $dir . "assets.php";
      $all_files[] = $dir . "html_document.php";
      $all_files[] = $dir . "loadall.php";
      $all_files[] = $dir . "misc.php";
      $all_files[] = $dir . "router.php";
      $all_files[] = $dir . "security.php";
      $all_files[] = $dir . "tag_matches.php";
      $all_files[] = $dir . "validator.php";
      $all_files[] = $dir . "view.php";
      $all_files[] = $dir . "database/model.php";
      $all_files[] = $dir . "database/paginate.php";
      $all_files[] = $dir . "traits/database/run_sql.php";
      $all_files[] = $dir . "traits/database/validation.php";
      $all_files[] = $dir . "traits/security/csrf_token_functions.php";
      $all_files[] = $dir . "traits/security/session_hijacking_functions.php";
      $i = 0;
      foreach ($all_files as $file) {
          $i++;
          $results = opcache_compile_file($file);
          if ($results) {
            echo "Added to cache, the file: $file" . PHP_EOL;
          } else {
            echo "FAILED to cache file: $file" . PHP_EOL;  
          }
          
      }
      echo "Total Compiled: $i" . PHP_EOL;
      echo PHP_EOL . "Done with OP-Cache Compile..." . PHP_EOL;
  }
    
  public function name_demo() {
      echo "Name Demo CLI...";
  }
  
  public function test($id) {
      echo "ID: $id";
  }
    
}