Skip to content

Commit

Permalink
mass updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Nov 11, 2023
1 parent 4a670ff commit 7a1a92c
Show file tree
Hide file tree
Showing 40 changed files with 276 additions and 2,610 deletions.
30 changes: 20 additions & 10 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,39 @@ on:
- push

jobs:
coding-standards:
continuous-integration-php-82:
name: "Coding Standards"

runs-on: ubuntu-latest

steps:
- name: "Checkout"
uses: actions/checkout@master

- name: "Install Php 8.3"
- name: "Install Php 8.2"
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
tools: composer:v2
extensions: openssl, json, pdo, pdo_mysql, fileinfo, curl

- name: "Validate composer.json"
run: php $(which composer) validate --strict

- name: "Install dependencies with composer"
run: php $(which composer) install --no-interaction --no-progress --no-suggest

- name: "Run PHP CodeSniffer"
run: php vendor/bin/phpcs --standard=phpcs.xml


continuous-integration-php-83:
name: "Coding Standards"
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: actions/checkout@master
- name: "Install Php 8.3"
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
tools: composer:v2
extensions: openssl, json, pdo, pdo_mysql, fileinfo, curl
- name: "Validate composer.json"
run: php $(which composer) validate --strict
- name: "Install dependencies with composer"
run: php $(which composer) install --no-interaction --no-progress --no-suggest
- name: "Run PHP CodeSniffer"
run: php vendor/bin/phpcs --standard=phpcs.xml
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
},
"require-dev": {
"squizlabs/php_codesniffer": "3.7.2",
"slevomat/coding-standard": "^8.13"
"slevomat/coding-standard": "^8.14"
},
"scripts": {
"post-create-project-cmd": [
Expand Down
6 changes: 5 additions & 1 deletion src/Auth/Roles/Interfaces/IdentityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@

interface IdentityInterface
{
public function getId() : int;
/**
* @return int
* @noinspection PhpMissingReturnTypeInspection
*/
public function getId();
}
5 changes: 2 additions & 3 deletions src/Console/Command/BuiltInWebServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use function chdir;
use function defined;
use function dirname;
use function error_clear_last;
use function escapeshellcmd;
use function exec;
use function fclose;
Expand Down Expand Up @@ -248,7 +247,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
$host = $input->getOption('host');
$host = strtolower(trim($host??'')?:'127.0.0.1');
$isIp = Ip::isValidIpv4($host);
$isLocal = $isIp && Ip::isLocalIP($host);
$isLocal = $isIp && Ip::isLocalIP4($host);
if (!$isIp && $host !== 'localhost') {
$output->writeln('');
$output->writeln(sprintf(
Expand Down Expand Up @@ -397,7 +396,7 @@ protected function doProcess(
);
return self::FAILURE;
}
set_error_handler(fn () => error_clear_last());
set_error_handler(static fn () => null);
$ports = $port === 'auto' ? range(8000, 9000) : [$port];
shuffle($ports);
array_unshift(
Expand Down
3 changes: 1 addition & 2 deletions src/Console/Command/ChecksumGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use function clearstatcache;
use function date;
use function dirname;
use function error_clear_last;
use function fclose;
use function filemtime;
use function fopen;
Expand Down Expand Up @@ -166,7 +165,7 @@ static function ($e) {
}


set_error_handler(static fn() => error_clear_last());
set_error_handler(static fn() => null);
try {
if (!$printOnly) {
if (!is_dir($checksumDirectory)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Container/ContainerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,16 @@ public function resolveCallable(
}
}

if (empty($arguments) && is_callable($callable)) {
if (is_callable($callable)) {
$arguments = $this
->resolveArguments(
(is_string($callable) || $callable instanceof Closure)
? new ReflectionFunction($callable)
: new ReflectionMethod(...$callable),
$arguments,
$arguments
);
}

$value = is_callable($callable) ? $callable(...$arguments) : $callable;
}
$this->allocateService($value);
Expand Down
2 changes: 1 addition & 1 deletion src/Container/Factory/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use ArrayAccess\TrayDigita\Benchmark\Interfaces\ProfilerInterface;
use ArrayAccess\TrayDigita\Benchmark\Profiler;
use ArrayAccess\TrayDigita\Benchmark\Waterfall;
use ArrayAccess\TrayDigita\Cache\Entities;
use ArrayAccess\TrayDigita\Cache\Cache;
use ArrayAccess\TrayDigita\Collection\Config;
use ArrayAccess\TrayDigita\Console\Application;
use ArrayAccess\TrayDigita\Container\Container;
Expand Down
17 changes: 17 additions & 0 deletions src/Event/Interfaces/ManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ public function getDispatchListener(): ?EventDispatchListenerInterface;
*/
public function attach(string $eventName, $eventCallback, int $priority = 10) : string;

/**
* Attach the event & then remove after dispatched
*
* @param string $eventName
* @param $eventCallback
* @param int $priority
* @return string
*/
public function attachOnce(string $eventName, $eventCallback, int $priority = 10) : string;

/**
* Check if has events
*
* @param string $eventName
* @param $eventCallback
* @return bool
*/
public function has(string $eventName, $eventCallback = null) : bool;

/**
Expand Down
47 changes: 35 additions & 12 deletions src/Event/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Manager implements ManagerInterface

protected array $events = [];

/**
* @var array<string, array<int, array<string, int>>>
*/
private array $eventOnce = [];

protected array $currents = [];

/**
Expand Down Expand Up @@ -159,8 +164,11 @@ public static function generateCallableId($eventCallback): array|null
];
}

public function attach(string $eventName, $eventCallback, int $priority = 10): string
{
public function attach(
string $eventName,
$eventCallback,
int $priority = 10
): string {
$callable = $this->generateCallableId($eventCallback);
if ($callable === null) {
throw new InvalidCallbackException(
Expand All @@ -174,6 +182,14 @@ public function attach(string $eventName, $eventCallback, int $priority = 10): s
return $id;
}

public function attachOnce(string $eventName, $eventCallback, int $priority = 10): string
{
$id = $this->attach(...func_get_args());
$this->eventOnce[$eventName][$priority][$id] ??= 0;
$this->eventOnce[$eventName][$priority][$id]++;
return $id;
}

public function has(
string $eventName,
$eventCallback = null
Expand Down Expand Up @@ -422,6 +438,7 @@ public function dispatch(
}

$this->currents[$eventName] ??= [];
// sorting
ksort($this->events[$eventName]);
// make temporary to prevent remove
$originalParam = $param;
Expand All @@ -433,21 +450,12 @@ public function dispatch(
}
foreach ($callableList as $id => $eventCallback) {
unset($eventCallback[$id]);

// prevent loops to call dispatch same
//if (isset($this->currents[$eventName][$priority][$id])) {
// continue;
//}

if (!isset($this->records[$eventName][$id][$priority])) {
$this->records[$eventName][$id][$priority] = 0;
}

foreach ($eventCallback as $inc => $callable) {
// prevent loops to call dispatch same increment
//if (isset($this->currents[$eventName][$priority][$id][$inc])) {
// continue;
//}
if (!isset($this->currents[$eventName][$priority][$id][$inc])) {
$this->currents[$eventName][$priority][$id][$inc] = 0;
}
Expand Down Expand Up @@ -482,7 +490,6 @@ public function dispatch(
&& is_object($callable[0])
&& is_string($callable[1] ?? null)
&& str_contains($callable[1], '::')
// && !is_callable($callable)
) {
$obj = $callable[0];
$methods = explode('::', $callable[1], 2);
Expand All @@ -509,7 +516,23 @@ public function dispatch(
$this->currentEvent = null;
$this->currentEventId = null;
$this->currentEventName = null;
// unset
unset($this->currents[$eventName][$priority][$id][$inc]);
if ($this->currents[$eventName][$priority][$id] === []) {
unset($this->currents[$eventName][$priority][$id]);
}
if (isset($this->eventOnce[$eventName][$priority][$id])) {
$this->eventOnce[$eventName][$priority][$id]--;
if ($this->eventOnce[$eventName][$priority][$id] <= 0) {
unset($this->eventOnce[$eventName][$priority][$id]);
}
if ([] === $this->eventOnce[$eventName][$priority]) {
unset($this->eventOnce[$eventName][$priority]);
}
if ([] === $this->eventOnce[$eventName]) {
unset($this->eventOnce[$eventName]);
}
}

// @onDispatched
$this->dispatchListener?->onFinishDispatch(
Expand Down
1 change: 1 addition & 0 deletions src/HttpKernel/Helper/AbstractHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace ArrayAccess\TrayDigita\HttpKernel\Helper;

use ArrayAccess\TrayDigita\Cache\Cache;
use ArrayAccess\TrayDigita\Cache\Entities;
use ArrayAccess\TrayDigita\Container\Interfaces\ContainerAllocatorInterface;
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerAllocatorInterface;
Expand Down
27 changes: 23 additions & 4 deletions src/Responder/FileResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct(SplFileInfo|string $file)
}
$this->file = $file;
$this->attachmentFileName = $this->file->getBasename();
$this->size = $this->valid() ? $this->file->getSize() : 0;
$this->size = $this->valid() ? ($this->file->getSize()?:0) : 0;
}

public function getFile(): SplFileInfo
Expand Down Expand Up @@ -409,9 +409,22 @@ private function sendRequestData(ServerRequestInterface $request) : never
$ranges = [];
// header for multi-bytes
$headers = [];
$total = 0;
$total = $fileSize;
$rangeTotal = 0;

// if empty
if ($total === 0) {
// set content length
$this->sendHeaderContentLength($total);
// send mimetype header
$this->sendHeaderMimeType();
// send etag
$this->sendHeaderEtag();
// send last modifier
$this->sendHeaderLastModified();
// send attachment header
$this->sendHeaderAttachment();
$this->stopRequest();
}
/**
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests
*/
Expand All @@ -425,6 +438,7 @@ private function sendRequestData(ServerRequestInterface $request) : never
$maxRangeRequest = $maxRange;
$minRangeRequest = null;
if ($maxRanges > 0 && $rangeHeader && preg_match('~^bytes=(.+)$~i', $rangeHeader, $match)) {
$total = 0;
$rangeHeader = array_map('trim', explode(',', trim($match[1])));
foreach ($rangeHeader as $range) {
$range = trim($range);
Expand Down Expand Up @@ -524,6 +538,7 @@ private function sendRequestData(ServerRequestInterface $request) : never
$this->sendHeader('Content-Range', "bytes $startingPoint-$end/$fileSize");
}
}

// set content length
$this->sendHeaderContentLength($total);
// send mimetype header
Expand All @@ -543,12 +558,16 @@ private function sendRequestData(ServerRequestInterface $request) : never

$sock = $this->getSock();
$sock->fseek($startingPoint);
while (!$sock->eof()) {
while (!$sock->eof() && $total > 0) {
$read = 4096;
if ($total < $read) {
$read = $total;
$total = 0;
}
// stop
if ($read < 1) {
break;
}
echo $sock->fread($read);
}
$this->stopRequest();
Expand Down
6 changes: 1 addition & 5 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,17 +763,13 @@ public function matchRouteParamByRequest(Route $route, ServerRequestInterface $r
$pattern = "#$pattern#";
}

set_error_handler(static function () {
error_clear_last();
});

set_error_handler(static fn () => null);
preg_match(
$pattern,
$path,
$match,
PREG_NO_ERROR
);

// restore
restore_error_handler();
if (empty($match)) {
Expand Down
16 changes: 16 additions & 0 deletions src/Util/Filter/Consolidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,22 @@ public static function callbackReduceError(
return $result;
}

/**
* Call the callble with hide the error
*
* @param callable $callback
* @param ...$args
*/
public static function callNoError(callable $callback, ...$args)
{
set_error_handler(static fn () => null);
try {
return $callback(...$args);
} finally {
restore_error_handler();
}
}

public static function namespace(string|object $fullClassName) : string|false
{
if (is_object($fullClassName)) {
Expand Down
1 change: 0 additions & 1 deletion src/Util/Filter/ContainerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public static function service(
)??Decorator::service($expect);
}


/**
* Get object instance or create if not available until interfaces
* beware using this method, it will check interface tree.
Expand Down
Loading

0 comments on commit 7a1a92c

Please sign in to comment.