summaryrefslogtreecommitdiff
path: root/core/cli
diff options
context:
space:
mode:
authorGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-04-27 03:47:30 +0300
committerGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-04-27 03:47:30 +0300
commit059e0d95d0c28bc5060e87e146eaf7411f51bf90 (patch)
tree7c21ac432f16dde2329a0d5954d70711eceb48e6 /core/cli
parent26cd8ee99659ef1926c96a049c93645ffc9b169d (diff)
downloadgyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.tar.gz
gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.tar.bz2
gyraf1gov-059e0d95d0c28bc5060e87e146eaf7411f51bf90.zip
skeleton commit; based on an anom project
Diffstat (limited to 'core/cli')
-rw-r--r--core/cli/cli-cache.php65
-rwxr-xr-xcore/cli/curl-cache.php155
-rw-r--r--core/cli/info.md23
-rw-r--r--core/cli/micro/setup.php120
-rw-r--r--core/cli/micro/term_utilities.php44
5 files changed, 407 insertions, 0 deletions
diff --git a/core/cli/cli-cache.php b/core/cli/cli-cache.php
new file mode 100644
index 0000000..57d8090
--- /dev/null
+++ b/core/cli/cli-cache.php
@@ -0,0 +1,65 @@
+#!/usr/local/bin/php
+<?php
+
+/** cli-cache
+ * regenerates/refreshes caches.
+ * ------------------------------------------------------
+ *
+ * The sctipt implements a micro-anom application
+ * that queries directry the project's models.
+ *
+ * Advantages vs curl-cache:
+ * ---
+ * + Runs out of the web/apache interface;
+ * + Has less requirements, uses less total resources;
+ * + Avoids the typical block/waiting/receiving delays;
+ * + (theoretically) it is a faster implementation
+ *
+ * ------------------------------------------------------
+ */
+$t0 = microtime(true); // time benchmark
+
+require_once 'micro/term_utilities.php'; // terminal colors
+
+echo textColor("\nInterface: ", NORMAL) . textColor(php_sapi_name(), GREEN). "\n";
+
+
+/** anom-Cli app initialization
+ * ------------------------------------------------------
+ */
+define('MINI_APP_BASE', __DIR__.'/');
+echo "Base directory is ". textColor(MINI_APP_BASE, GREEN) ."\n";
+
+require_once MINI_APP_BASE.'/micro/setup.php';
+echo "System is ready;\n";
+
+
+
+try {
+ # your code goes here ------------------------------------------------------
+
+
+ // Re-create category_products tree Cache
+ app\controllers\cli\CliContoller::cacheCategoriesTree();
+
+ $categories = app\controllers\cli\CliContoller::getCategories();
+
+
+ foreach($categories as $key => $category) {
+
+ echo textColor( textWidth($category['Title'], 72), NORMAL);
+ app\controllers\cli\CliContoller::cacheCategoryByUrl($category['FullFriendlyUrl']);
+ }
+
+ # end of script ------------------------------------------------------------
+
+} catch(Exception $e) { // Basic error handling
+ echo 'Caught exception: ', textColor($e->getMessage(), ERROR), "\n";
+ die();
+}
+
+
+$dt = number_format( microtime(true) - $t0 , 0) ."sec";
+
+// echo ending...
+echo textColor("\nOperation Completed {$dt}\n\n", NORMAL);
diff --git a/core/cli/curl-cache.php b/core/cli/curl-cache.php
new file mode 100755
index 0000000..6ab2e93
--- /dev/null
+++ b/core/cli/curl-cache.php
@@ -0,0 +1,155 @@
+#!/usr/local/bin/php
+<?php
+
+/** curl-cache
+ * regenerates/refreshes caches
+ * ------------------------------------------------------
+ *
+ * This scipt makes requests to the project's web API,
+ * These API calls SHOULD be impemented to cache some
+ * resource-expensive queries, so in result the script
+ * shall manage to create cache.
+ *
+ * NOTE:
+ * The script does not implement the anom framerork
+ * and it is provided for documentation puproses.
+ *
+ * ------------------------------------------------------
+ */
+
+// some defines and an into message
+// -----------------------------------------------------------------------------
+
+require_once 'micro/term_utilities.php'; // terminal utilites
+
+define('API_ROOT','http://127.0.0.1:8080/api/');
+
+define('BATCH_LENGTH', 3); // see notes later on the script
+
+
+
+echo "\033[32mInterface: ". php_sapi_name() ."\033[39m\n\n";
+
+$t0 = microtime(true);
+
+/** supplumentary functions (curl)
+ * -----------------------------------------------------------------------------
+ */
+
+/** single Curl get call
+ *
+ * @param $call (string): url that needs to be called
+ *
+ */
+function singleCurl($call) {
+ $cSRC = curl_init(); // create curl resource for SouRCe end-point
+ curl_setopt($cSRC, CURLOPT_URL, API_ROOT.$call); // set url
+ curl_setopt($cSRC, CURLOPT_RETURNTRANSFER, 1); //return the transfer as a string
+ $output = curl_exec($cSRC); // $output contains the output string in json format
+ curl_close($cSRC); // close curl resource to free up system resources
+
+ $json = json_decode( $output ); // decode json to php array
+
+ if (!$json->success) {
+ echo "\033[91mError reading " . $call ."\n\033[39m";
+ return false;
+ }
+
+ return $json;
+}
+
+/** multiple curl get calls
+ *
+ * @param $array: an array of urls to be called
+ *
+ */
+function multiCurl($array) {
+
+ $multiCurl = array(); // array of curl handlers
+ $result = array(); // data to be returned
+ $mh = curl_multi_init(); // multi handler
+ $module = array(); // array of module names / 1:1 to $result array
+
+ foreach ($array as $i => $category) {
+ $module[$i] = $category->ID; // guid (module name)
+ $fetchURL = API_ROOT .'url/'. $category->FullFriendlyUrl;
+ $multiCurl[$i] = curl_init();
+ curl_setopt($multiCurl[$i], CURLOPT_URL,$fetchURL);
+ curl_setopt($multiCurl[$i], CURLOPT_HEADER,0);
+ curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER,1);
+ curl_multi_add_handle($mh, $multiCurl[$i]);
+ }
+
+ $index=null;
+
+ do {
+ curl_multi_exec($mh,$index);
+ } while($index > 0);
+
+ foreach($multiCurl as $k => $ch) {
+ $result[$k] = curl_multi_getcontent($ch);
+ curl_multi_remove_handle($mh, $ch);
+ }
+ curl_multi_close($mh); // close multi-curl
+}
+
+// force printing string with specified length
+function forceLen($str, $len = 72) {
+ $space = " ";
+ for($i = 0 ; $i<$len ; $i++) $space .= ".";
+ return mb_substr($str.$space, 0, $len-1) ." ";
+}
+
+
+/** main procedure
+ * -----------------------------------------------------------------------------
+ */
+
+echo "\033[39mReading Tree of product categories...\n";
+$tree = singleCurl('tree'); // ask for categories tree
+
+if ($tree !==false) echo "\033[32mCategories tree is cached\033[39m\n\n";
+
+$categories = singleCurl('category');
+
+// request every category to create cache if not exist
+// send requests in small batches to avoid enormous server-stress
+echo "\033[39mRequesting re-Cache for every category\033[39m\n";
+echo "\033[33mPlease Wait...\n\n";
+
+$buffer = []; // buffer array
+$counter = 0; // counter to track items in buffer
+foreach($categories->result as $category) {
+
+ echo "\033[39m". forceLen($category->Title) ."\033[33m"."Sent.\n\033[39m";
+
+ $counter++;
+
+ // try several batch lengths to establish the ideal one that produces
+ // results in a smooth quite fase rthm, without stressing the server;
+ // so that visitors can navigate the production site white the script
+ // is running;
+ // for my testing this number is 3-5. I set the cou 3 items give fast results without stressing the server
+ // so "if ($counter > 2) { ... " is the smoothest option
+ if ($counter > 2) {
+ multiCurl($buffer);
+ $counter = 0;
+ $buffer = [];
+ }
+
+ $buffer[] = $category;
+
+}
+
+multiCurl($buffer); // run once more for the last non-filled buffer
+
+$dt = number_format( microtime(true) - $t0 , 1) ."sec";
+
+// echo ending...
+echo "\n\033[32mOperation Completed \033[39m({$dt})\n\n";
+echo "\033[39m"; // back to default color;
+
+
+// check
+// http://www.idein.it/joomla/14-docker-php-apache-with-crontab
+?> \ No newline at end of file
diff --git a/core/cli/info.md b/core/cli/info.md
new file mode 100644
index 0000000..39f8c11
--- /dev/null
+++ b/core/cli/info.md
@@ -0,0 +1,23 @@
+# cli
+
+This section is used to setup an anom micro-framework for the cli-interface.
+The cli interface is commonly used to support and optimize the whole app.
+
+A cli script may run manualy, for example using...
+
+ php cli-cache.php
+
+although...
+
+it is recommended to run periodically using the cron service:
+
+ 15 * * * * php path/to/cli-cache.php
+
+or ...
+
+when a docker instanse of the project fires up; for example including
+the following line of code inside the docker/bin/docker-entrypoint.sh
+(runs the script after 5 minutes delay to make sure that the instance
+is up and running)
+
+ at -f /var/www/core/cli/refresh-cache.php -t now +5 minutes
diff --git a/core/cli/micro/setup.php b/core/cli/micro/setup.php
new file mode 100644
index 0000000..ca2fd40
--- /dev/null
+++ b/core/cli/micro/setup.php
@@ -0,0 +1,120 @@
+<?php
+/** Micro anom framework
+ * minimum setup with the rich features of the framework;
+ *
+ * in a symbolic way: { Micro_anom summarizes anom }
+ * -----------------------------------------------------------------------------
+ *
+ * (+) Use it mainly in cli-interface scripts to
+ * access your application's data intarnaly
+ *
+ * (+) Implement routine tasks and schedule the
+ * execution using cron deamon
+ *
+ * (+) Avoid public/web-based API calls
+ *
+ * (+) Create batch proccessing scripts re-using
+ * your project's implemented methods;
+ *
+ * Don't haves
+ * ---
+ * In your cli-interfaced micro-framework you most
+ * probably won't need some of the anom's central
+ * objects (Request, Router, Session, Contollers).
+ *
+ * Controller is the script, Router is the scheduler,
+ * Session and Authentication is unnecessary (just
+ * put an...
+ * ```if (php_sapi_name() != 'cli') return false;```
+ * and no call will access the code but the cli.
+ *
+ *
+ * What you do have?
+ * ---
+ * + Database and Cache objects,
+ * + all the implemented methods,
+ * + Proxy, Repositories etc...
+ *
+ * and these are more than enough to write easily
+ * a fast, secure, powerful script that handles
+ * your data in every way you need.
+ */
+
+echo textColor("Setup environment....", GREEN) ."\n";
+
+// get constants
+// -----------------------------------------------------------------------------
+require_once realpath(MINI_APP_BASE.'../config/constants.php');
+
+// anom typical defines
+// -----------------------------------------------------------------------------
+define('PRODUCTION', false);
+
+define('APP_NAME', 'e-Classroom'); // Application Name
+
+define('APP_VERSION', 'v0.1'); // Application version
+
+define('APP_ROOT', realpath(MINI_APP_BASE.'../../public'));
+
+// Cache driver and caching TTLs
+// -----------------------------------------------------------------------------
+define('CACHE_DRIVER', 'FileCache');
+
+// Root objects like product-categories tree
+define('CACHE_ROOT_TTL', 28800); // 8 hours
+
+// Product Category
+define('CACHE_CATEGORY_TTL', 18000); // 5 hours
+
+// Product
+define('CACHE_PRODUCT_TTL', 3600); // 1 hour
+
+define('FILECACHE_PATH', realpath(MINI_APP_BASE.'../../storage/cache/').'/');
+
+echo "Cache Directory is ". textColor(FILECACHE_PATH, GREEN)."\n";
+
+
+// database
+// -----------------------------------------------------------------------------
+
+$ini_array = parse_ini_file(realpath(MINI_APP_BASE."../auth/.env"));
+define('DB_NAME', $ini_array['DB_NAME']);
+define('DB_USER', $ini_array['DB_USER']);
+define('DB_PASS', $ini_array['DB_PASS']);
+define('PDO_HOST', $ini_array['PDO_HOST']);
+echo "Application database is ". textColor(DB_NAME, GREEN) ."\n";
+
+define('DB_TIMEZONE', "SET time_zone = 'Europe/Athens'");
+
+
+// load autoloader
+// -----------------------------------------------------------------------------
+require_once realpath(MINI_APP_BASE.'../../vendor/autoload.php');
+
+
+
+// Attache database to the Registry a vow
+Registry::vow('database', function() { return new Database(); });
+
+
+// Attach Cache to the Resitry as a vow
+Registry::vow('cache', function() { return new (CACHE_DRIVER)(); });
+
+
+require_once realpath(MINI_APP_BASE.'../helpers/design_patterns.php');
+
+
+# Registry::set('reg', 'Registry is working');
+# echo Registry::get('reg'), "\n\n";
+
+
+# NOTE:
+# php does not provide error and exception handling
+# for the cli interface; you still can handle the
+# exceptions using standard try {...} catch { }
+#
+# try {
+# // code
+# } catch (Exception $exc) {
+# echo 'Caught exception: ', $exc->getMessage(), "\n"
+# } \ No newline at end of file
diff --git a/core/cli/micro/term_utilities.php b/core/cli/micro/term_utilities.php
new file mode 100644
index 0000000..971c36e
--- /dev/null
+++ b/core/cli/micro/term_utilities.php
@@ -0,0 +1,44 @@
+<?php
+
+define('NORMAL', "\033[39m"); // white
+define('SUCCESS', "\033[32m"); // green
+define('FAIL', "\033[1;31m"); // red
+define('ERROR', "\033[1;31m"); // red
+define('PROGRESS', "\033[33m"); // orenge
+
+define('WHITE', "\033[39m"); // white
+define('GREEN', "\033[32m"); // green
+define('RED', "\033[1;31m"); // red
+define('ORANGE', "\033[33m"); // orenge
+
+
+function textColor($text, $mode = NORMAL) {
+ switch ($mode) {
+ case SUCCESS:
+ case GREEN:
+ return SUCCESS.$text.NORMAL;
+ break;
+
+ case FAIL:
+ case RED:
+ return FAIL.$text.NORMAL;
+ break;
+
+ case PROGRESS:
+ case ORANGE:
+ return PROGRESS.$text.NORMAL;
+ break;
+
+ default:
+ return NORMAL.$text;
+ }
+}
+
+
+// force printing string with specified length
+function textWidth($str, $len = 72) {
+ $space = " ";
+ for($i = 0 ; $i<$len ; $i++) $space .= ".";
+
+ return mb_substr($str.$space, 0, $len-1) ." ";
+} \ No newline at end of file