Skip to content

MaplePHP Stable v1 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Auth/Middleware/SessionStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

class SessionStart implements MiddlewareInterface
{
const NAME = NULL; // Set a custom seesion name
const TIME = 360;
const SSL = true;
const SAMESITE = true;
public const NAME = null; // Set a custom seesion name
public const TIME = 360;
public const SSL = true;
public const SAMESITE = true;

private $container;

Expand Down
176 changes: 176 additions & 0 deletions Cli/Connectors/AbstractCli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

namespace MaplePHP\Foundation\Cli\Connectors;

use MaplePHP\Foundation\DocComment\Comment;
use MaplePHP\Foundation\Http\Provider;
use MaplePHP\Http\Interfaces\RequestInterface;
use MaplePHP\Prompts\Prompt;
use MaplePHP\Prompts\Command;
use ReflectionException;

abstract class AbstractCli implements CliInterface
{
protected array $args;
protected array $protocol;
protected Prompt $prompt;
protected Command $command;
protected Provider $provider;
private array $input = [];

public function __construct(RequestInterface $request, Provider $provider)
{
$this->args = $request->getCliArgs();
$this->prompt = new Prompt();
$this->command = new Command();
$this->protocol = static::PROMPT;
$this->provider = $provider;
}

/**
* Extend the prompt
* @param string $promptName
* @param array $arr
* @return void
*/
protected function addPrompt(string $promptName, array $arr): void
{
$this->protocol[$promptName] = $arr;
}

/**
* Check if prompt exists
* @param string $promptName
* @return bool
*/
protected function hasPrompt(string $promptName): bool
{
return isset($this->protocol[$promptName]);
}

/**
* Filter out cli command for hidden and passed argv
* @param array $arr
* @return array
*/
protected function filterSetArgs(array $arr): array
{
$new = [];
foreach ($arr as $key => $val) {
if(!isset($this->args[$key])) {
$type = ($val['type'] ?? "");
if($type === "hidden") {
$this->input[$key] = ($val['default'] ?? "");
} else {
$new[$key] = $val;
}
} else {
$this->input[$key] = $this->args[$key];
}
}
return $new;
}

/**
* Get filtered values
* @param array $arr
* @return array
*/
protected function getFilterVal(array $arr): array
{
return array_merge($arr, $this->input);
}

/**
* Extract help text with doc block comment
* @param object|string $className
* @return Comment
* @throws ReflectionException
*/
protected function getCommentBlock(object|string $className): Comment
{
$block = new Comment($className);
$this->command->title($block->getClass(true));
$methods = $block->getAllMethods();
foreach ($block->getAllMethods() as $method) {
$comment = $block->getDocComment($method);
if(isset($comment['description'])) {
$class = strtolower($block->getClass(true));
if(($comment['methodExtends'][0] ?? "") === "prompt") {
foreach($this->protocol as $meth => $_arr) {
if(!in_array($meth, $methods)) {
$this->generateHelpText($comment, $class, $meth);
}
}

} else {
$this->generateHelpText($comment, $class, $method);
}
}

}
return $block;
}

/**
* Will help generate help texts in PHP Maple Cli
* Example: Extending Prompt Array items
* --help:
* description: Add parameter types usage example to prompt help text
* default: Add Default value to parameter usage example to prompt help text
* help: Force remove parameter type from usage example in prompt help text
* @param array $comment
* @param string $class
* @param string $method
* @return void
*/
private function generateHelpText(array $comment, string $class, string $method)
{
$classA = "$class:$method";
$length = strlen($classA);
$padA = str_pad("", 40 - $length, " ", STR_PAD_RIGHT);
$padB = str_pad("", 40, " ", STR_PAD_RIGHT);

if(isset($this->protocol[$method])) {
$valid = reset($this->protocol[$method]);
//isValidPrompt
if(isset($valid['type']) && isset($valid['message'])) {

$this->command->statusMsg($classA, false);
$this->command->message("{$padA}{$comment['description']}");

$fill = "{$padB} ";
$this->command->title("{$fill}Arguments");
foreach ($this->protocol[$method] as $arg => $data) {
if((bool)($data['help'] ?? true) !== false) {
$message = (!empty($data['description'])) ? $data['description'] : ($data['message'] ?? "");
$addEg = (!empty($data['default'])) ? " (example: {$data['default']})" : "";
$this->command->approve("$fill--$arg: ", false);
$this->command->message($message . $addEg);
}
}
$this->command->message("\n");
}
}
}

private function isValidPrompt()
{
}

/**
* Access help (or with added flag --help)
* @return void
* @throws ReflectionException
*/
public function help(): void
{
$this->command->title("\nMaplePHP CLI Commands");
$this->command->message("Below are all the available inputs for the MaplePHP CLI commands.\n");
$block = $this->getCommentBlock($this);
$class = strtolower($block->getClass(true));
$this->command->title("\nExample usage: ", false);
$this->command->message("php cli $class:[action] --arg=1 --arg=2");
}

}
2 changes: 1 addition & 1 deletion Cli/Connectors/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(ContainerInterface $container, RequestInterface $req

public function install()
{

$type = ($this->args['type'] ?? null);
$envConfig = $this->cli->getConfig();
$allowedConfigs = array_keys($envConfig);
Expand Down
2 changes: 1 addition & 1 deletion Cli/Connectors/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function (StreamInterface $_stream) use ($delete) {

public function insertUser()
{
$orgs = $this->user->getAllOrgs("org_id,org_name", function($row) {
$orgs = $this->user->getAllOrgs("org_id,org_name", function ($row) {
return [$row->org_id => $row->org_name];
});

Expand Down
27 changes: 18 additions & 9 deletions Cli/Connectors/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* This is NOT COMPLETED!
* THERE is MUCH to more to be done!
*/

namespace MaplePHP\Foundation\Cli\Connectors;

use MaplePHP\Foundation\Cli\Connectors\CliInterface;
Expand Down Expand Up @@ -58,16 +59,22 @@ public function resize()
$this->img = new Resize($original);
$valid = new Inp($hex);
if($valid->equal("auto") || $valid->hex()) {
if($hex === "auto") $hex = $this->img->getAverageColour();
if($opacity > 1) $opacity = 0;
if($opacity > 0) $hex = $this->img->getLuminance($hex, $opacity);
if($hex === "auto") {
$hex = $this->img->getAverageColour();
}
if($opacity > 1) {
$opacity = 0;
}
if($opacity > 0) {
$hex = $this->img->getLuminance($hex, $opacity);
}
$hex = str_replace("&", "#", $hex);
if($this->img->isMime("image/png")) {
$this->img->setBackgroundColor($hex, $opacity);
}
}

$set = array();
$set = [];
$path = "{$savePath}{$basename}-0";
$this->resizeImg($type, $path, 2600, 2600);
$this->img->execute();
Expand All @@ -82,7 +89,7 @@ public function resize()
$this->resizeImg($type, $path, $width, $heigth);
$this->img->convertTo("webp");
$this->img->execute();
$set[$width+$heigth] = $this->img->getTruePath();
$set[$width + $heigth] = $this->img->getTruePath();
}

$this->cli->write("Image has been resized and optimized.");
Expand Down Expand Up @@ -120,16 +127,18 @@ protected function resizeImg(string $type, string $file, int $width, int $height
switch($type) {
case "resize":
$this->img->resize($width, $height);
break;
break;
case "crop":
$this->img->crop($width, $height);
break;
break;
case "trim":
$this->img->trim();
$this->img->resize($width, $height);
break;
break;
}
if(!$this->img->isMime("image/gif")) {
$this->img->convertTo("jpg");
}
if(!$this->img->isMime("image/gif")) $this->img->convertTo("jpg");
//$fallbackImg = $this->img->getTrueBasename();
}

Expand Down
Loading