4 Commits
4.3 ... 4.6

Author SHA1 Message Date
User
133371a908 20250228#2 2025-02-28 13:39:44 +03:00
User
49ae47e43e 20250228#1 2025-02-28 13:11:14 +03:00
User
8fc94aaeb8 20250224#1 2025-02-24 19:45:12 +03:00
User
aba089db5e 20250218#1 2025-02-18 02:39:16 +03:00
5 changed files with 66 additions and 16 deletions

View File

@@ -9,5 +9,5 @@ composer create-project rmphp/skeleton project-name
```
```bash
composer create-project rmphp/skeleton:"^4.3" project-name
composer create-project rmphp/skeleton:"^4.6" project-name
```

View File

@@ -2,26 +2,40 @@
namespace Base\Application;
use Exception;
use ReflectionClass;
use Rmphp\Storage\Component\AbstractDataObject;
abstract class AbstractDTO extends AbstractDataObject {
/**
* @param array|object ...$data
* @return static
* @throws Exception
*/
public static function fromData(array|object ...$data) : static {
$array = array_map(function($item) {
return (is_object($item)) ? get_object_vars($item) : $item;
}, $data);
return self::fromArray(array_merge(...$array));
}
/**
* @param object $data
* @return static
* @throws Exception
*/
public static function fromObject(object $data) : static {
return self::fromArray(get_object_vars($data));
}
abstract class AbstractDTO {
/**
* @param array $data
* @return static
* @throws Exception
*/
static function fromArray(array $data) : static {
$class = new ReflectionClass(static::class);
$self = new static();
foreach ($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($class->hasMethod('set'.ucfirst($property->getName()))) $self->{'set'.ucfirst($property->getName())}($value);
// прямое присовение по умолчанию
elseif(isset($value)) $self->{$property->getName()} = $value;
}
return $self;
public static function fromArray(array $data) : static {
return self::fillObject(new ReflectionClass(static::class), new static(), $data);
}
}

3
application/bin/cli Normal file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/php
<?php
require_once dirname(__FILE__).'/cli.php';

33
application/bin/cli.php Normal file
View File

@@ -0,0 +1,33 @@
<?php
use DI\Container;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;use Rmphp\Kernel\ResponseEmitter;
require_once dirname(__DIR__,2).'/vendor/autoload.php';
(new Symfony\Component\Dotenv\Dotenv())->usePutenv()->loadEnv(dirname(__DIR__,2).'/.env');
/** @var LoggerInterface $logger */
$logger = require_once dirname(__DIR__,2).'/application/config/components/loggerFactory.php';
/** @var Container $container */
$container = require_once dirname(__DIR__,2).'/application/config/components/containerFactory.php';
$container->set(LoggerInterface::class, $logger);
if(str_contains($argv[1], ':')) {
list($className, $method) = explode(':', $argv[1]);
if(class_exists($className)) {
try {
$controllers = $container->get($className);
if(method_exists($controllers, $method)) {
$response = $controllers->$method();
}
if(isset($response)) {
if($response instanceof ResponseInterface) (new ResponseEmitter())->emit($response);
elseif(!is_bool($response)) echo $response;
}
} catch(Throwable $throwable){
$logger->error($throwable->getMessage()." on ".$throwable->getFile().":".$throwable->getLine());
}
}
else echo "Class $className does not exist";
}

View File

@@ -17,7 +17,7 @@
"rmphp/router": "^1.2",
"rmphp/session": "^1.1",
"rmphp/redis": "^1.0",
"rmphp/storage": "^5.0",
"rmphp/storage": "^6.0",
"symfony/dotenv": "^6.2"
},
"autoload": {