Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
133371a908 | ||
|
|
49ae47e43e | ||
|
|
8fc94aaeb8 | ||
|
|
aba089db5e |
@@ -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
|
||||
```
|
||||
|
||||
@@ -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
3
application/bin/cli
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
require_once dirname(__FILE__).'/cli.php';
|
||||
33
application/bin/cli.php
Normal file
33
application/bin/cli.php
Normal 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";
|
||||
}
|
||||
@@ -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": {
|
||||
|
||||
Reference in New Issue
Block a user