Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
remils committed May 26, 2023
0 parents commit b6aa862
Show file tree
Hide file tree
Showing 30 changed files with 998 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
20 changes: 20 additions & 0 deletions LICENSE
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.
11 changes: 11 additions & 0 deletions README.md
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.
19 changes: 19 additions & 0 deletions composer.json
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"
}
}
27 changes: 27 additions & 0 deletions src/Bus/Bus.php
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;
}
}
}
}
}
10 changes: 10 additions & 0 deletions src/Bus/Event.php
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;
}
10 changes: 10 additions & 0 deletions src/Bus/Subscriber.php
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;
}
51 changes: 51 additions & 0 deletions src/Config/Config.php
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);
}
}
9 changes: 9 additions & 0 deletions src/Config/ConfigException.php
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
{
}
75 changes: 75 additions & 0 deletions src/Container/Container.php
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.');
}
}
9 changes: 9 additions & 0 deletions src/Container/ContainerException.php
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
{
}
31 changes: 31 additions & 0 deletions src/Database/Connection.php
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);
}
}
69 changes: 69 additions & 0 deletions src/Database/Database.php
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;
}
}
}
9 changes: 9 additions & 0 deletions src/Database/DatabaseException.php
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
{
}
Loading

0 comments on commit b6aa862

Please sign in to comment.