Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11b1544317 | ||
|
|
9e4ae35455 | ||
|
|
23133c173c | ||
|
|
bf529d83c8 |
@@ -21,3 +21,4 @@ PAGE501="/templates/error/501.tpl"
|
||||
|
||||
# Users environment
|
||||
MYSQL_PARAM='{"host":"host.docker.internal", "user":"***user***", "pass":"***password***","base":"***basename***", "logsEnable":true}'
|
||||
REDIS_PARAM='{"host":"127.0.0.1","port":6379,"connectTimeout":2.5,"backoff":{"algorithm":1,"base":500,"cap":750}, "database":0, "defaultExpire":300}'
|
||||
|
||||
@@ -9,5 +9,5 @@ composer create-project rmphp/skeleton project-name
|
||||
```
|
||||
|
||||
```bash
|
||||
composer create-project rmphp/skeleton:"^4.1" project-name
|
||||
composer create-project rmphp/skeleton:"^4.2" project-name
|
||||
```
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
*/
|
||||
|
||||
namespace Base\Domain;
|
||||
|
||||
abstract class AbstractObject implements EntityInterface {
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId(): mixed {
|
||||
return (isset($this->id)) ? (($this->id instanceof ValueObjectInterface) ? $this->id->get() : $this->id) : null;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
* Date: 23.04.2024
|
||||
* Time: 3:58
|
||||
*/
|
||||
|
||||
namespace Base\Domain;
|
||||
|
||||
interface EntityInterface {
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId(): mixed;
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
* Date: 23.04.2024
|
||||
* Time: 3:58
|
||||
*/
|
||||
|
||||
namespace Base\Domain;
|
||||
|
||||
interface ValueObjectInterface {
|
||||
|
||||
public function get();
|
||||
public function __toString(): string;
|
||||
|
||||
}
|
||||
0
application/base/Repository/.gitkeep
Normal file
0
application/base/Repository/.gitkeep
Normal file
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Repository;
|
||||
|
||||
use Base\Domain\EntityInterface;
|
||||
use Rmphp\Storage\Mysql\MysqlStorageInterface;
|
||||
|
||||
abstract class AbstractMysqlRepository extends AbstractRepository {
|
||||
|
||||
public const DEBUG = false;
|
||||
|
||||
public function __construct(
|
||||
public readonly MysqlStorageInterface $mysql
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param EntityInterface $object
|
||||
* @param string $table
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function saveEntity(EntityInterface $object, string $table) : mixed {
|
||||
$in = $this->getProperties($object, function ($value){
|
||||
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
|
||||
});
|
||||
if(static::DEBUG) dd($object, $in, $table);
|
||||
try {
|
||||
if (!empty($object->getId()) && !empty($this->mysql->findById($table, $object->getId()))) {
|
||||
$this->mysql->updateById($table, $in, $object->getId());
|
||||
return $object->getId();
|
||||
} else {
|
||||
$this->mysql->insert($table, $in);
|
||||
return (is_string($object->getId())) ? $object->getId() : $this->mysql->mysql()->insert_id;
|
||||
}
|
||||
} catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
### Создание объекта из массива
|
||||
|
||||
```php
|
||||
setProperties(array $data) : void
|
||||
```
|
||||
|
||||
|
||||
### Получение массива из объекта
|
||||
|
||||
```php
|
||||
getProperties(callable $method = null) : array
|
||||
```
|
||||
@@ -1,93 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
* Date: 25.04.2024
|
||||
* Time: 13:06
|
||||
*/
|
||||
|
||||
namespace Base\Repository;
|
||||
|
||||
use Base\Domain\ValueObjectInterface;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
|
||||
class AbstractRepository {
|
||||
|
||||
static array $classes = [];
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function create(string $class, $data) : mixed {
|
||||
try {
|
||||
if(!isset(static::$classes[$class])) static::$classes[$class] = new ReflectionClass($class);
|
||||
$object = new $class;
|
||||
foreach (static::$classes[$class]->getProperties() as $property) {
|
||||
$propertyNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
|
||||
// data[propertyName] ?? data[property_name] ?? null
|
||||
$value = $data[$property->getName()] ?? $data[$propertyNameSnakeCase] ?? null;
|
||||
|
||||
// если есть внутренний метод (приоритетная обработка)
|
||||
if(static::$classes[$class]->hasMethod('set'.ucfirst($property->getName()))) $object->{'set'.ucfirst($property->getName())}($value);
|
||||
// Если тип свойства класс (valueObject)
|
||||
elseif($property->hasType() && class_exists($property->getType()->getName())) $object->{$property->getName()} = new ($property->getType()->getName())($value);
|
||||
// если значения не пустое
|
||||
elseif(isset($value)) $object->{$property->getName()} = $value;
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
catch (ReflectionException $exception) {
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object $class
|
||||
* @param callable|null $method
|
||||
* @return array
|
||||
*/
|
||||
public function getProperties(object $class, callable $method = null) : array {
|
||||
|
||||
$objectData = get_object_vars($class);
|
||||
foreach ($objectData as $fieldName => $value)
|
||||
{
|
||||
// to option_id
|
||||
$fieldNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $fieldName));
|
||||
|
||||
// если есть внутренний метод (приоритетная обработка)
|
||||
if(method_exists($this, 'get'.ucfirst($fieldName))) {
|
||||
$out[$fieldNameSnakeCase] = $this->{'get'.ucfirst($fieldName)}($value);
|
||||
}
|
||||
// если тип свойства класс (valueObject)
|
||||
elseif($value instanceof ValueObjectInterface && null !== $value->get()) {
|
||||
$out[$fieldNameSnakeCase] = $value->get();
|
||||
}
|
||||
// если передана callable функция через которую нужно пропустить все элементы
|
||||
elseif(isset($method) && !is_array($value) && !is_object($value)) {
|
||||
$out[$fieldNameSnakeCase] = $method($value);
|
||||
}
|
||||
// если это логическое значение
|
||||
elseif(is_bool($value)){
|
||||
$out[$fieldNameSnakeCase] = (int) $value;
|
||||
}
|
||||
// если это дробное число
|
||||
elseif(is_float($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
// если это целое число
|
||||
elseif(is_int($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
// если это строка
|
||||
elseif(is_string($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
return $out ?? [];
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Repository;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class RepositoryException extends \Exception {
|
||||
|
||||
public array $data;
|
||||
|
||||
public function __construct($message="", $code=0, array $data = [], Throwable $previous=null) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<?php
|
||||
return (new \Rmphp\Content\Content('/templates/base.tpl'))->setSubtemplatePath('/templates/')->setSubtemplatePathAlias([
|
||||
"main" => "/src/Infrastructure/templates",
|
||||
"main" => "/src/Infrastructure/subtemplates",
|
||||
]);
|
||||
|
||||
@@ -2,14 +2,11 @@
|
||||
|
||||
use Rmphp\Cache\Cache;
|
||||
use Rmphp\Cache\CacheInterface;
|
||||
use Rmphp\Session\Session;
|
||||
use Rmphp\Session\SessionInterface;
|
||||
use Rmphp\Storage\Mysql\MysqlStorage;
|
||||
use Rmphp\Storage\Mysql\MysqlStorageInterface;
|
||||
|
||||
return [
|
||||
MysqlStorageInterface::class => DI\create(MysqlStorage::class)->constructor(json_decode(getenv("MYSQL_PARAM"), true)),
|
||||
SessionInterface::class => DI\create(Session::class)->constructor(),
|
||||
CacheInterface::class => DI\create(Cache::class)->constructor("../var/cache"),
|
||||
'App\*\Domain\Repository\*RepositoryInterface' => DI\autowire('App\*\Infrastructure\Repository\*Repository'),
|
||||
];
|
||||
];
|
||||
|
||||
@@ -8,6 +8,6 @@
|
||||
# Empty
|
||||
- key: "<@any>"
|
||||
routes:
|
||||
- action: "App\\nfrastructure\\Controllers\\IndexController"
|
||||
- action: "App\\Infrastructure\\Controllers\\IndexController"
|
||||
method: "emptyAction"
|
||||
params: ""
|
||||
|
||||
@@ -12,12 +12,12 @@
|
||||
"monolog/monolog": "^2.3",
|
||||
"php-di/php-di": "^6.3",
|
||||
"ramsey/uuid": "^4.7",
|
||||
"rmphp/cache-file": "^1.0",
|
||||
"rmphp/content": "^3.1",
|
||||
"rmphp/kernel": "^4.1",
|
||||
"rmphp/kernel": "^5.0",
|
||||
"rmphp/router": "^1.2",
|
||||
"rmphp/session": "^1.0",
|
||||
"rmphp/storage": "^3.1",
|
||||
"rmphp/redis": "^1.0",
|
||||
"rmphp/storage": "^4.0",
|
||||
"symfony/dotenv": "^6.2"
|
||||
},
|
||||
"autoload": {
|
||||
|
||||
Reference in New Issue
Block a user