Skip to content

Commit

Permalink
feat: add once method and new improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed May 20, 2024
1 parent 8f11efb commit ccb857f
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/Core/Facades/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ public function bind(string $abstract, Closure|string $bind): void
$bind = $result;
}

if (is_string($bind)) {
$bind = $this->singleton($bind);
}

$this->objectPool[$abstract] = $bind;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Core/Facades/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ private function coreMiddleware(object|null $controller = null, string|null $fun
* Process middleware and controller.
*
* @param array<string, mixed> $route
* @return Respond|Stream
* @return mixed
*
* @throws ErrorException
*/
private function process(array $route): Respond|Stream
private function process(array $route): mixed
{
$controller = $route['controller'];
$function = $route['function'];
Expand Down Expand Up @@ -124,11 +124,11 @@ private function process(array $route): Respond|Stream
/**
* Run route list.
*
* @return Respond|Stream
* @return mixed
*
* @throws HttpException
*/
private function runRoute(): Respond|Stream
private function runRoute(): mixed
{
$path = $this->request->getValidUrl();
$method = $this->request->getValidMethod();
Expand Down
8 changes: 3 additions & 5 deletions src/Core/Http/Respond.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public function transform(mixed $respond): Respond

if ($respond instanceof Respond) {
$this->setCode($respond->getCode());
$this->content = $respond->getContent();
$this->setContent($respond->getContent());
$this->headers = new Header([...$this->headers->all(), ...$respond->headers->all()]);
$this->setParameter([...$this->getParameter(), ...$respond->getParameter()]);

Expand Down Expand Up @@ -552,10 +552,8 @@ public function send(mixed $respond): void

if ($respond instanceof Stream) {
$respond->push();
}

if ($respond instanceof Respond && $respond->getContent()) {
fwrite($respond->getStream(), $respond->getContent(false));
} else {
fwrite($this->stream, $this->getContent(false));
}

// Send output buffer.
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function __construct(Cookie $cookie)
$this->cookie = $cookie;
$this->data = new Header();

$this->name = env('APP_NAME', 'kamu') . static::SESSID;
$this->name = strtolower(env('APP_NAME', 'kamu') . static::SESSID);
$this->expires = intval(env('COOKIE_LIFETIME', 120));

if (env('COOKIE', 'true') == 'true') {
Expand Down
6 changes: 3 additions & 3 deletions src/Core/Http/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private function pushSingle(string $range): Closure
/**
* Send multi file.
*
* @param array $ranges
* @param array<int, string> $ranges
* @return Closure
*/
private function pushMulti(array $ranges): Closure
Expand Down Expand Up @@ -210,7 +210,7 @@ private function pushMulti(array $ranges): Closure
* Get range file.
*
* @param string $range
* @return array
* @return array<int, int>
*
* @throws StreamTerminate
*/
Expand Down Expand Up @@ -402,7 +402,7 @@ public function process(): Stream
*/
public function push(): void
{
($this->callback)();
call_user_func($this->callback);
}

/**
Expand Down
70 changes: 70 additions & 0 deletions src/Core/Support/Once.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Core\Support;

use Closure;
use Core\Facades\App;
use ReflectionFunction;
use WeakMap;

class Once
{
/**
* @var WeakMap<object, array<string, mixed>> $values
*/
protected $values;

/**
* Create a new once instance.
*
* @return void
*/
public function __construct()
{
$this->values = new WeakMap();
}

/**
* Get the value of the callable.
*
* @param array<int, array<string, mixed>> $trace
* @param callable $callable
* @return mixed
*/
public function &value(array $trace, callable $callable): mixed
{
if (str_contains($trace[0]['file'] ?? '', 'eval()\'d code')) {
return null;
}

$uses = array_map(
fn (mixed $argument): mixed => is_object($argument) ? spl_object_hash($argument) : $argument,
$callable instanceof Closure ? (new ReflectionFunction($callable))->getClosureUsedVariables() : [],
);

$hash = crc32(sprintf(
'%s@%s%s:%s',
$trace[0]['file'],
isset($trace[1]['class']) ? ($trace[1]['class'] . '@') : '',
$trace[1]['function'],
$trace[0]['line']
));

$fn = sprintf('%d(%d)', $hash, crc32(serialize($uses)));

$objectName = $trace[1]['object'] ?? $this;
$object = App::get()->singleton($objectName::class);

if (isset($this->values[$object][$fn])) {
return $this->values[$object][$fn];
}

if (!isset($this->values[$object])) {
$this->values[$object] = [];
}

$this->values[$object][$fn] = call_user_func($callable);

return $this->values[$object][$fn];
}
}
17 changes: 16 additions & 1 deletion src/helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function event(object $event): object
* Baca dari file .kita serta masih bentuk object.
*
* @param string $path
* @param array $data
* @param array<string, mixed> $data
* @return \Core\View\Render
*/
function render(string $path, array $data = []): \Core\View\Render
Expand Down Expand Up @@ -667,3 +667,18 @@ function fake(string $locale = 'id_ID'): \Faker\Generator
return \Core\Facades\App::get()->singleton(\Faker\Factory::class);
}
}

if (!function_exists('once')) {
/**
* Panggil secara satu kali saja.
*
* @template TReturnType
*
* @param callable(): TReturnType $callback
* @return TReturnType
*/
function &once(callable $callback): mixed
{
return \Core\Facades\App::get()->singleton(\Core\Support\Once::class)->value(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2), $callback);
}
}

0 comments on commit ccb857f

Please sign in to comment.