-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b6aa862
Showing
30 changed files
with
998 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2023 Sergey Zatulivetrov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# RUFY | ||
|
||
## Install | ||
|
||
```ssh | ||
composer require remils/rufy | ||
``` | ||
|
||
## License | ||
|
||
Copyright (c) Zatulivetrov Sergey. Distributed under the MIT. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "remils/rufy", | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"Remils\\Rufy\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Sergey Zatulivetrov", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Remils\Rufy\Bus; | ||
|
||
final class Bus | ||
{ | ||
/** @var array<Subscriber> */ | ||
protected array $subscribers = []; | ||
|
||
public function subscriber(Subscriber $subscriber): void | ||
{ | ||
$this->subscribers[] = $subscriber; | ||
} | ||
|
||
public function dispatch(Event $event): void | ||
{ | ||
foreach ($this->subscribers as $subscriber) { | ||
if ($subscriber->getEventName() === $event->getName()) { | ||
$subscriber->handle($event); | ||
|
||
if ($event->stopPropagation()) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Remils\Rufy\Bus; | ||
|
||
interface Event | ||
{ | ||
public function stopPropagation(): bool; | ||
|
||
public function getName(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Remils\Rufy\Bus; | ||
|
||
interface Subscriber | ||
{ | ||
public function getEventName(): string; | ||
|
||
public function handle(Event $event): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Remils\Rufy\Config; | ||
|
||
use Throwable; | ||
|
||
final class Config | ||
{ | ||
protected array $config = []; | ||
|
||
public function __construct(string $path) | ||
{ | ||
$this->boot($path); | ||
} | ||
|
||
protected function boot(string $path): void | ||
{ | ||
$content = file_get_contents($path); | ||
|
||
preg_match_all('/^([A-Z\_]{1,})\=(.*)$/m', $content, $matches, PREG_SET_ORDER); | ||
|
||
foreach ($matches as $matche) { | ||
try { | ||
$key = $matche[1]; | ||
$value = json_decode($matche[2], true, 512, JSON_THROW_ON_ERROR); | ||
} catch (Throwable $exception) { | ||
throw new ConfigException('Decoding error "' . $key . '" in the config.'); | ||
} | ||
|
||
if ($this->has($key)) { | ||
throw new ConfigException('Config "' . $key . '" already exists.'); | ||
} | ||
|
||
$this->config[$key] = $value; | ||
} | ||
} | ||
|
||
public function get(string $key): mixed | ||
{ | ||
if ($this->has($key)) { | ||
return $this->config[$key]; | ||
} | ||
|
||
throw new ConfigException('Config "' . $key . '" not found.'); | ||
} | ||
|
||
protected function has(string $key): bool | ||
{ | ||
return array_key_exists($key, $this->config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Remils\Rufy\Config; | ||
|
||
use Exception; | ||
|
||
final class ConfigException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Remils\Rufy\Container; | ||
|
||
use ReflectionFunction; | ||
|
||
final class Container | ||
{ | ||
protected array $dependencies = []; | ||
|
||
public function bind(string $key, mixed $value): void | ||
{ | ||
if ($this->has($key)) { | ||
throw new ContainerException('Dependency "' . $key . '" already exists.'); | ||
} | ||
|
||
if (is_callable($value)) { | ||
$this->dependencies[$key] = $this->resolve($value); | ||
} else { | ||
$this->dependencies[$key] = $value; | ||
} | ||
} | ||
|
||
public function get(string $key): mixed | ||
{ | ||
if ($this->has($key)) { | ||
return $this->dependencies[$key]; | ||
} | ||
|
||
$this->throwNotFound($key); | ||
} | ||
|
||
public function has(string $key): bool | ||
{ | ||
return array_key_exists($key, $this->dependencies); | ||
} | ||
|
||
public function resolve(callable $callback): mixed | ||
{ | ||
$args = []; | ||
|
||
$reflectionFunction = new ReflectionFunction($callback); | ||
$reflectionParameters = $reflectionFunction->getParameters(); | ||
|
||
foreach ($reflectionParameters as $reflectionParameter) { | ||
$name = $reflectionParameter->getName(); | ||
/** @var ReflectionNamedType|null */ | ||
$type = $reflectionParameter->getType(); | ||
|
||
if ($type && !$type->isBuiltin() && $this->has($type->getName())) { | ||
$args[$name] = $this->get($type->getName()); | ||
continue; | ||
} | ||
|
||
if ($this->has($name)) { | ||
$args[$name] = $this->get($name); | ||
continue; | ||
} | ||
|
||
if ($reflectionParameter->isOptional()) { | ||
$args[$name] = $reflectionParameter->getDefaultValue(); | ||
continue; | ||
} | ||
|
||
$this->throwNotFound($name); | ||
} | ||
|
||
return $reflectionFunction->invokeArgs($args); | ||
} | ||
|
||
protected function throwNotFound(string $key): void | ||
{ | ||
throw new ContainerException('Dependency "' . $key . '" not found.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Remils\Rufy\Container; | ||
|
||
use Exception; | ||
|
||
final class ContainerException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Remils\Rufy\Database; | ||
|
||
final class Connection | ||
{ | ||
protected array $connections = []; | ||
|
||
public function init(string $key, Database $database): void | ||
{ | ||
if ($this->has($key)) { | ||
throw new DatabaseException('Database "' . $key . '" already exists.'); | ||
} | ||
|
||
$this->connections[$key] = $database; | ||
} | ||
|
||
public function connect(string $key): Database | ||
{ | ||
if ($this->has($key)) { | ||
return $this->connections[$key]; | ||
} | ||
|
||
throw new DatabaseException('Database "' . $key . '" not found.'); | ||
} | ||
|
||
protected function has(string $key): bool | ||
{ | ||
return array_key_exists($key, $this->connections); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Remils\Rufy\Database; | ||
|
||
use PDO; | ||
use ReflectionFunction; | ||
use Throwable; | ||
|
||
final class Database | ||
{ | ||
protected PDO $pdo; | ||
|
||
public function __construct(string $dsn, string $username = null, string $password = null) | ||
{ | ||
$this->pdo = new PDO($dsn, $username, $password); | ||
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
} | ||
|
||
public function execute(string $query, array $params = []): bool | ||
{ | ||
$stmt = $this->pdo->prepare($query); | ||
|
||
return $stmt->execute($params); | ||
} | ||
|
||
public function fetch(string $entity, string $query, array $params = []): ?object | ||
{ | ||
$stmt = $this->pdo->prepare($query); | ||
$stmt->execute($params); | ||
$stmt->setFetchMode(PDO::FETCH_CLASS, $entity); | ||
|
||
return $stmt->fetch() ?? null; | ||
} | ||
|
||
public function fetchAll(string $entity, string $query, array $params = []): array | ||
{ | ||
$stmt = $this->pdo->prepare($query); | ||
$stmt->execute($params); | ||
$stmt->setFetchMode(PDO::FETCH_CLASS, $entity); | ||
|
||
return $stmt->fetchAll(); | ||
} | ||
|
||
public function fetchColumn(string $query, array $params = [], int $column = 0): mixed | ||
{ | ||
$stmt = $this->pdo->prepare($query); | ||
$stmt->execute($params); | ||
|
||
return $stmt->fetchColumn($column); | ||
} | ||
|
||
public function transaction(callable $callback, ...$args): mixed | ||
{ | ||
try { | ||
$this->pdo->beginTransaction(); | ||
|
||
$reflectionFunction = new ReflectionFunction($callback); | ||
$result = $reflectionFunction->invoke($this, ...$args); | ||
|
||
$this->pdo->commit(); | ||
|
||
return $result; | ||
} catch (Throwable $exception) { | ||
$this->pdo->rollBack(); | ||
|
||
throw $exception; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Remils\Rufy\Database; | ||
|
||
use Exception; | ||
|
||
final class DatabaseException extends Exception | ||
{ | ||
} |
Oops, something went wrong.