Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
133371a908 | ||
|
|
49ae47e43e | ||
|
|
8fc94aaeb8 |
@@ -9,5 +9,5 @@ composer create-project rmphp/skeleton project-name
|
|||||||
```
|
```
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
composer create-project rmphp/skeleton:"^4.4" project-name
|
composer create-project rmphp/skeleton:"^4.6" project-name
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -2,48 +2,40 @@
|
|||||||
|
|
||||||
namespace Base\Application;
|
namespace Base\Application;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
use Rmphp\Storage\Component\AbstractDataObject;
|
||||||
|
|
||||||
abstract class AbstractDTO {
|
abstract class AbstractDTO extends AbstractDataObject {
|
||||||
|
|
||||||
/**
|
|
||||||
* @param object $data
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
static function fromObject(object $data) : static {
|
|
||||||
return static::fromArray(get_object_vars($data));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|object ...$data
|
* @param array|object ...$data
|
||||||
* @return static
|
* @return static
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
static function fromData(array|object ...$data) : static {
|
public static function fromData(array|object ...$data) : static {
|
||||||
$array = array_map(function($item) {
|
$array = array_map(function($item) {
|
||||||
return (is_object($item)) ? get_object_vars($item) : $item;
|
return (is_object($item)) ? get_object_vars($item) : $item;
|
||||||
}, $data);
|
}, $data);
|
||||||
return static::fromArray(array_merge(...$array));
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return static
|
* @return static
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
static function fromArray(array $data) : static {
|
public static function fromArray(array $data) : static {
|
||||||
$class = new ReflectionClass(static::class);
|
return self::fillObject(new ReflectionClass(static::class), new static(), $data);
|
||||||
$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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
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";
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user