Commit e26048f2 authored by Ognjen Cvetković's avatar Ognjen Cvetković

Merge branch 'development' into 'master'

siler update

See merge request !17
parents 5e56ed71 393d4231
This diff is collapsed.
......@@ -17,7 +17,7 @@ use function Siler\array_get;
*/
function init(string $path): array
{
$dot_env = Dotenv::create($path);
$dot_env = Dotenv::createImmutable($path);
return $dot_env->load();
}
......
......@@ -14,7 +14,8 @@ use function json_encode;
* @param int $depth
* @return string
*/
function encode($value, int $options = JSON_THROW_ON_ERROR, int $depth = 512): string
//function encode($value, int $options = JSON_THROW_ON_ERROR, int $depth = 512): string
function encode($value, int $options = JSON_BIGINT_AS_STRING, int $depth = 512): string
{
return json_encode($value, $options, $depth);
}
......
......@@ -14,7 +14,8 @@ use UnexpectedValueException;
*/
function env_var(string $key, ?string $default = null): string
{
$value = getenv($key);
/** @var string|false $value */
$value = $_ENV[$key] ?? false;
if ($value === false) {
if ($default === null) {
......
This diff is collapsed.
......@@ -31,4 +31,9 @@ final class Field
* @psalm-var string|null
*/
public $listOf;
/**
* @var boolean
* @psalm-var boolean
*/
public $nullable = false;
}
This diff is collapsed.
......@@ -104,7 +104,7 @@ function url(?string $path = null): string
*/
function path(): string
{
/** @var array<string, string> $_SERVER */
/** @psalm-var array<string, string> $_SERVER */
$script_name = array_get_str($_SERVER, 'SCRIPT_NAME', '');
$query_string = array_get_str($_SERVER, 'QUERY_STRING', '');
$request_uri = array_get_str($_SERVER, 'REQUEST_URI', '');
......@@ -114,6 +114,7 @@ function path(): string
$script_name = '';
}
$request_uri = rawurldecode($request_uri);
$request_uri = str_replace('?' . $query_string, '', $request_uri);
$script_path = str_replace('\\', '/', dirname($script_name));
......
......@@ -33,3 +33,19 @@ function set(array &$array, string $key, $value)
$array[array_shift($keys)] = $value;
return $array;
}
/**
* Creates an array of associative arrays using the first element as keys.
*
* @param array $arr
* @return array
*/
function assoc(array $arr): array
{
/** @var array<array-key, array-key> $head */
$head = $arr[0];
return array_map(static function (array $row) use ($head): array {
return array_combine($head, $row);
}, array_slice($arr, 1));
}
<?php declare(strict_types=1);
namespace Siler\Prelude;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
/**
* Class Dispatcher
* @package Siler\Event
*/
final class Dispatcher implements EventDispatcherInterface, ListenerProviderInterface
{
/**
* @var array<class-string, callable[]>
*/
private $listeners = [];
/**
* Listen for an event using its class name as pattern.
*
* @template E
* @param string $eventClass
* @psalm-param class-string $eventClass
* @param callable(E):void $callback
* @return $this
*/
public function listen(string $eventClass, callable $callback): self
{
if (empty($this->listeners[$eventClass])) {
$this->listeners[$eventClass] = [];
}
$this->listeners[$eventClass][] = $callback;
return $this;
}
/**
* Dispatch the given event.
*
* @param object $event
* @return object
*/
public function dispatch(object $event): object
{
$event_class = get_class($event);
foreach ($this->listeners[$event_class] as $callback) {
$callback($event);
}
return $event;
}
/**
* Returns the listeners for the given event.
*
* @param object $event
* @return iterable
*/
public function getListenersForEvent(object $event): iterable
{
$event_class = get_class($event);
return $this->listeners[$event_class];
}
}
......@@ -2,22 +2,23 @@
namespace Siler\Prelude;
use ReflectionObject;
use function Siler\Str\snake_case;
/**
* Trait FromArray
* @package Siler\Prelude
* @template T
*/
trait FromArray
{
/**
* @param array $arr
* @return static
* @return mixed
* @psalm-return T
*/
public static function fromArray(array $arr): self
public static function fromArray(array $arr)
{
$obj = new static();
$reflection = new \ReflectionObject($obj);
$obj = new self();
$reflection = new ReflectionObject($obj);
foreach ($reflection->getProperties() as $prop) {
$prop_name = $prop->getName();
......
<?php declare(strict_types=1);
namespace Siler\Prelude;
/**
* @template T
*/
interface FromArrayInterface
{
/**
* @param array $data
* @return mixed
* @psalm-return T
*/
public static function fromArray(array $data);
}
<?php declare(strict_types=1);
namespace Siler\IO;
/**
* Prints a string to the output and adds an EOL.
*
* @param string $str
*/
function println(string $str): void
{
echo $str, PHP_EOL;
}
/**
* Returns a CSV-file contents as an array.
*
* @param string $filename
* @param string $delimiter
* @param int $length
* @return array
*/
function csv_to_array(string $filename, string $delimiter = ',', int $length = 0): array
{
$handle = fopen($filename, 'r');
$arr = [];
while ($row = fgetcsv($handle, $length, $delimiter)) {
$arr[] = $row;
}
fclose($handle);
return $arr;
}
......@@ -5,17 +5,17 @@ namespace Siler\Prelude;
use function Siler\Obj\patch;
/**
* OO interface for `Obj/patch`.
* @package Siler\Prelude
* @template T
*/
trait Patch
{
/**
* @param array $arr
* @return $this
* @param array $data
* @return mixed
* @psalm-return T
*/
public function patch(array $arr): self
public function patch(array $data)
{
return patch($this, $arr);
return patch($this, $data);
}
}
<?php declare(strict_types=1);
namespace Siler\Prelude;
/**
* @template T
*/
interface PatchInterface
{
/**
* @param array $data
* @return mixed
* @psalm-return T
*/
public function patch(array $data);
}
<?php declare(strict_types=1);
namespace Siler\Prelude;
interface ToArrayInterface
{
public function toArray(): array;
}
......@@ -5,7 +5,16 @@
namespace Siler\Route;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use ReflectionParameter;
use RegexIterator;
use Siler\Container;
use Siler\Http;
use Siler\Http\Request;
......@@ -189,6 +198,7 @@ function regexify(string $path): string
];
$path = preg_replace(array_keys($patterns), array_values($patterns), $path);
/** @var string $base */
$base = Container\get(BASE_PATH, '');
......@@ -306,12 +316,12 @@ function files(string $basePath, string $prefix = '', $request = null)
$realpath = realpath($basePath);
if (false === $realpath) {
throw new \InvalidArgumentException("{$basePath} does not exists");
throw new InvalidArgumentException("{$basePath} does not exists");
}
$directory = new \RecursiveDirectoryIterator($realpath);
$iterator = new \RecursiveIteratorIterator($directory);
$regex = new \RegexIterator($iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
$directory = new RecursiveDirectoryIterator($realpath);
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
$files = array_keys(iterator_to_array($regex));
......@@ -357,15 +367,15 @@ function files(string $basePath, string $prefix = '', $request = null)
* @param array{0: string, 1: string}|ServerRequestInterface|null $request
*
* @return void
* @throws \ReflectionException
* @throws ReflectionException
*
*/
function class_name(string $basePath, $className, $request = null): void
{
$reflection = new \ReflectionClass($className);
$reflection = new ReflectionClass($className);
$object = $reflection->newInstance();
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
$specs = preg_split('/(?=[A-Z])/', $method->name);
......@@ -376,7 +386,7 @@ function class_name(string $basePath, $className, $request = null): void
return $segment != 'index';
});
$path_params = array_map(function (\ReflectionParameter $param) {
$path_params = array_map(function (ReflectionParameter $param) {
$param_name = $param->getName();
$param_name = "{{$param_name}}";
......
......@@ -18,10 +18,7 @@ const DEFAULT_STRATIGILITY_PIPELINE = 'default_stratigility_pipeline';
*
* @param ServerRequestInterface $request The PSR-7 request.
* @param string $name The pipeline name.
*
* @return Closure
*
* @return Closure(callable):Closure(array):ResponseInterface
* @return Closure(callable(ServerRequestInterface, array<array-key, mixed>):ResponseInterface):Closure
*/
function process(ServerRequestInterface $request, string $name = DEFAULT_STRATIGILITY_PIPELINE): Closure
{
......
......@@ -200,6 +200,12 @@ const emergency = '\Siler\Monolog\emergency';
namespace Siler\Arr;
const set = '\Siler\Arr\set';
const assoc = '\Siler\Arr\assoc';
namespace Siler\IO;
const println = '\Siler\IO\println';
const csv_to_array = '\Siler\IO\csv_to_array';
namespace Siler\Klass;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment