- WIP on some pushMessage and createdAt/updatedAt handling
This commit is contained in:
1 parent
993c556d30
commit
382a4459d7
5 files changed
+68
-38
No files matched your search
@@ -58,7 +58,9 @@ class EntityGenerate
|
||||
$webPath = $this->pathFromNamespace($this->webNamespace, $this->application->routes);
|
||||
|
||||
if (empty($webPath)) {
|
||||
throw new \InvalidArgumentException("Could not find web path routes from given namespaces.");
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf("Could not find web path routes from given namespaces. Given info : %s, %s", $this->webNamespace, json_encode($this->application->routes))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +68,9 @@ class EntityGenerate
|
||||
$apiPath = $this->pathFromNamespace($this->apiNamespace, $this->application->routes);
|
||||
|
||||
if (empty($apiPath)) {
|
||||
throw new \InvalidArgumentException("Could not find web path routes from given namespaces.");
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf("Could not find web path routes from given namespaces. Given info : %s, %s", $this->apiNamespace, json_encode($this->application->routes))
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->includeDocs) {
|
||||
@@ -101,12 +105,12 @@ class EntityGenerate
|
||||
}
|
||||
|
||||
if (isset($formPath)) {
|
||||
if ($this->saveForm) {
|
||||
if ($this->saveForm ?? false) {
|
||||
$this->writeToFile($formPath, $this->saveForm, $this->compiledSaveForm,);
|
||||
$this->writeToFile($formPath, $this->saveContext, $this->compiledSaveFormContext,);
|
||||
}
|
||||
|
||||
if ($this->deleteForm) {
|
||||
if ($this->deleteForm ?? false) {
|
||||
$this->writeToFile($formPath, $this->deleteForm, $this->compiledDeleteForm,);
|
||||
$this->writeToFile($formPath, $this->deleteContext, $this->compiledDeleteFormContext,);
|
||||
}
|
||||
|
||||
+51
-25
@@ -26,6 +26,11 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
|
||||
|
||||
protected string $messageWarningSave = 'lean.api.form.save.warning.entity';
|
||||
|
||||
protected array $fields = [
|
||||
'updatedAt' => 'updatedAt',
|
||||
'createdAt' => 'createdAt'
|
||||
];
|
||||
|
||||
protected array $reflectedProperties;
|
||||
|
||||
protected string $contextClass = FormContext::class;
|
||||
@@ -72,6 +77,8 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
|
||||
}
|
||||
}
|
||||
|
||||
$this->handleTimestamp($entity);
|
||||
|
||||
try {
|
||||
$this->assignContextToEntity($context);
|
||||
|
||||
@@ -112,38 +119,38 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
|
||||
{
|
||||
$entity = $this->getEntity();
|
||||
|
||||
foreach($entity::resolveEntity()->fieldList() as $key => $property) {
|
||||
$field = $property->getAttribute(Field::class)->object;
|
||||
foreach($entity::resolveEntity()->getPropertyHavingAttribute(EntityField::class) as $key => $entityField) {
|
||||
$property = $entity::resolveEntity()->reflectedClass->getProperties(true)[$key];
|
||||
|
||||
if (! $field->readonly || ! $entity->isLoaded()) {
|
||||
if ($property) {
|
||||
if (($property->getAttribute(Field::class)->object->readonly ?? false) && $entity->isLoaded()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$apiField = $property->getAttribute(EntityField::class)->object ?? null;
|
||||
$apiField = $entityField[0]->object ;
|
||||
$var = $apiField->field ?: $key;
|
||||
|
||||
if ($apiField) {
|
||||
$var = $apiField->field ?: $key;
|
||||
|
||||
if ( property_exists($context, $var) && ( new \ReflectionProperty($context, $var) )->isInitialized($context) ) {
|
||||
if ($apiField->setterMethod) {
|
||||
# Use a setter method
|
||||
call_user_func([ $entity, $apiField->setterMethod ], $context->{$var});
|
||||
}
|
||||
else {
|
||||
# Direct property set
|
||||
$entity->$key = $context->{$var};
|
||||
}
|
||||
if ( property_exists($context, $var) && ( new \ReflectionProperty($context, $var) )->isInitialized($context) ) {
|
||||
if ($apiField->setterMethod) {
|
||||
# Use a setter method
|
||||
call_user_func([ $entity, $apiField->setterMethod ], $context->{$var});
|
||||
}
|
||||
elseif (! $entity->isLoaded()) {
|
||||
# Applies 'default' value assigned to ContextField property
|
||||
$attr = $this->reflectedProperties($context)[$var] ?? null;
|
||||
else {
|
||||
# Direct property set
|
||||
$entity->$key = $context->{$var};
|
||||
}
|
||||
}
|
||||
elseif (! $entity->isLoaded()) {
|
||||
# Applies 'default' value assigned to ContextField property
|
||||
$attr = $this->reflectedProperties($context)[$var] ?? null;
|
||||
|
||||
if ($attr) {
|
||||
$contextField = $attr->getAttribute(ContextField::class);
|
||||
if ($attr) {
|
||||
$contextField = $attr->getAttribute(ContextField::class);
|
||||
|
||||
if ($contextField) {
|
||||
if (isset($contextField->object->default)) {
|
||||
if ($contextField) {
|
||||
if (isset($contextField->object->default)) {
|
||||
|
||||
$entity->$key = $contextField->object->default;
|
||||
}
|
||||
$entity->$key = $contextField->object->default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,6 +159,25 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
|
||||
}
|
||||
}
|
||||
|
||||
protected function handleTimestamp(EntityInterface $entity) : void
|
||||
{
|
||||
if ($entity->isLoaded()) {
|
||||
if (property_exists($entity, $this->fields['updatedAt']) && $entity->repository()->generateDatasetDiff($entity) ) {
|
||||
$field = $this->fields['updatedAt'];
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (property_exists($entity, $this->fields['createdAt']) && empty($entity->{$this->fields['createdAt']})) {
|
||||
$field = $this->fields['createdAt'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($field ?? false) {
|
||||
$cls = $entity::resolveEntity()->field($field)->type->type;
|
||||
$entity->{$field} = new $cls();
|
||||
}
|
||||
}
|
||||
|
||||
private function reflectedProperties(FormContextInterface $context) : array
|
||||
{
|
||||
$this->reflectedProperties ??= ObjectReflection::fromClass($context::class)->reflectProperties(\ReflectionProperty::IS_PUBLIC);
|
||||
|
||||
@@ -26,25 +26,25 @@ class FormContext extends \Picea\Ui\Method\FormContext {
|
||||
return $this->languageHandler->languageFromKey($key, $variables);
|
||||
}
|
||||
|
||||
public function pushSuccessMessage($key, $variables = []) {
|
||||
public function pushSuccessMessage($key, $variables = []) : void {
|
||||
$this->pushMessage($this->message::generateSuccess(
|
||||
$this->lang($key, $variables)
|
||||
));
|
||||
}
|
||||
|
||||
public function pushWarningMessage($key, $variables = []) {
|
||||
public function pushWarningMessage($key, $variables = []) : void {
|
||||
$this->pushMessage($this->message::generateWarning(
|
||||
$this->lang($key, $variables)
|
||||
));
|
||||
}
|
||||
|
||||
public function pushErrorMessage($key, $variables = []) {
|
||||
public function pushErrorMessage($key, $variables = []) : void {
|
||||
$this->pushMessage($this->message::generateError(
|
||||
$a = $this->lang($key, $variables)
|
||||
));
|
||||
}
|
||||
|
||||
public function pushInfoMessage($key, $variables = []) {
|
||||
public function pushInfoMessage($key, $variables = []) : void {
|
||||
$this->pushMessage($this->message::generateInfo(
|
||||
$this->lang($key, $variables)
|
||||
));
|
||||
|
||||
@@ -15,8 +15,6 @@ class CORS implements MiddlewareInterface {
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
|
||||
{
|
||||
|
||||
var_dump($request->getMethod() );
|
||||
if ($request->getMethod() === "option") {
|
||||
die("an option :)");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user