From 0460b0076eb152a2051c31e418cb1fbcd271bd25 Mon Sep 17 00:00:00 2001 From: Nicolas Alexandre Date: Tue, 12 Nov 2024 17:56:27 +0100 Subject: [PATCH] feat: improve cache of eloquent datasource --- .../BaseDatasource/src/BaseDatasource.php | 14 +++++++-- .../src/EloquentCollection.php | 29 +++++++++++++++++++ .../src/EloquentDatasource.php | 22 ++++++++++++++ .../src/CollectionMethods.php | 4 --- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/packages/BaseDatasource/src/BaseDatasource.php b/packages/BaseDatasource/src/BaseDatasource.php index dc67cc17..29dc1a05 100644 --- a/packages/BaseDatasource/src/BaseDatasource.php +++ b/packages/BaseDatasource/src/BaseDatasource.php @@ -83,16 +83,24 @@ private function makeDoctrineConnection(array $databaseConfig): void /** * @codeCoverageIgnore */ - public function __sleep(): array + public function __serialize(): array { - return ['collections', 'charts', 'databaseConfig']; + return [ + 'collections' => $this->collections, + 'charts' => $this->charts, + 'databaseConfig' => $this->databaseConfig, + ]; } /** * @codeCoverageIgnore */ - public function __wakeup(): void + public function __unserialize(array $data): void { + $this->collections = $data['collections']; + $this->charts = $data['charts']; + $this->databaseConfig = $data['databaseConfig']; + $this->makeOrm($this->databaseConfig); $this->makeDoctrineConnection($this->databaseConfig); } diff --git a/packages/DatasourceEloquent/src/EloquentCollection.php b/packages/DatasourceEloquent/src/EloquentCollection.php index a8d3032d..d16b4040 100644 --- a/packages/DatasourceEloquent/src/EloquentCollection.php +++ b/packages/DatasourceEloquent/src/EloquentCollection.php @@ -338,4 +338,33 @@ private function nullablePolymorphicMorphManyRelationFields($record, $relationNa // Do nothing } } + + public function __serialize(): array + { + return [ + 'fields' => $this->fields, + 'actions' => $this->actions, + 'segments' => $this->segments, + 'charts' => $this->charts, + 'schema' => $this->schema, + 'dataSource' => $this->dataSource, + 'name' => $this->name, + 'tableName' => $this->tableName, + 'model' => $this->model::class, + ]; + } + + public function __unserialize(array $data): void + { + $this->fields = $data['fields']; + $this->actions = $data['actions']; + $this->segments = $data['segments']; + $this->charts = $data['charts']; + $this->schema = $data['schema']; + $this->dataSource = $data['dataSource']; + $this->name = $data['name']; + $this->tableName = $data['tableName']; + $this->model = new $data['model'](); + $this->nativeDriver = null; + } } diff --git a/packages/DatasourceEloquent/src/EloquentDatasource.php b/packages/DatasourceEloquent/src/EloquentDatasource.php index ce248a8e..090daf94 100644 --- a/packages/DatasourceEloquent/src/EloquentDatasource.php +++ b/packages/DatasourceEloquent/src/EloquentDatasource.php @@ -69,4 +69,26 @@ public function getModels(): array { return $this->models; } + + /** + * @codeCoverageIgnore + */ + public function __serialize(): array + { + return array_merge( + parent::__serialize(), + ['supportPolymorphicRelations' => $this->supportPolymorphicRelations] + ); + } + + /** + * @codeCoverageIgnore + */ + public function __unserialize(array $data): void + { + parent::__unserialize($data); + + $finder = new ClassFinder(config('projectDir')); + $this->models = $finder->getModelsInNamespace('App'); + } } diff --git a/packages/DatasourceToolkit/src/CollectionMethods.php b/packages/DatasourceToolkit/src/CollectionMethods.php index 4f9c2227..57bc7076 100644 --- a/packages/DatasourceToolkit/src/CollectionMethods.php +++ b/packages/DatasourceToolkit/src/CollectionMethods.php @@ -60,10 +60,6 @@ public function setSegments(array $segments): Collection */ public function addField(string $name, ColumnSchema|RelationSchema $field): void { - if ($this->fields->has($name)) { - throw new ForestException('Field ' . $name . ' already defined in collection'); - } - $this->fields->put($name, $field); }