-
-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split schemaEntity into storage and metadata classes
- Loading branch information
Showing
12 changed files
with
289 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Civi\Schema; | ||
|
||
abstract class EntityMetadataBase implements EntityMetadataInterface { | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected string $entityName; | ||
|
||
public function __construct(string $entityName) { | ||
$this->entityName = $entityName; | ||
} | ||
|
||
protected function getEntity(): array { | ||
return EntityRepository::getEntity($this->entityName); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Civi\Schema; | ||
|
||
interface EntityMetadataInterface { | ||
|
||
public function getProperty(string $propertyName); | ||
|
||
public function getFields(): array; | ||
|
||
public function getOptions(string $fieldName, array $values = NULL): ?array; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Civi\Schema; | ||
|
||
final class EntityProvider { | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private string $entityName; | ||
|
||
/** | ||
* @var EntityMetadataInterface | ||
*/ | ||
private $meta; | ||
|
||
/** | ||
* @var EntityStorageInterface | ||
*/ | ||
private $storage; | ||
|
||
public function __construct(string $entityName) { | ||
$this->entityName = $entityName; | ||
} | ||
|
||
public function getMeta(string $property) { | ||
return $this->getMetaProvider()->getProperty($property); | ||
} | ||
|
||
public function getFields(): array { | ||
return $this->getMetaProvider()->getFields(); | ||
} | ||
|
||
public function getField(string $fieldName): ?array { | ||
return $this->getFields()[$fieldName] ?? NULL; | ||
} | ||
|
||
public function getOptions(string $fieldName, array $values = NULL): ?array { | ||
return $this->getMetaProvider()->getOptions($fieldName, $values); | ||
} | ||
|
||
public function writeRecords(array $records): array { | ||
return $this->getStorageProvider()->writeRecords($records); | ||
} | ||
|
||
public function deleteRecords(array $records): array { | ||
return $this->getStorageProvider()->deleteRecords($records); | ||
} | ||
|
||
private function getMetaProvider(): EntityMetadataInterface { | ||
if (!isset($this->meta)) { | ||
$entity = EntityRepository::getEntity($this->entityName); | ||
if (isset($entity['metaProvider'])) { | ||
return new $entity['metaProvider']($this->entityName); | ||
} | ||
if (isset($entity['fields'])) { | ||
return new SqlEntityMetadata($this->entityName); | ||
} | ||
if (isset($entity['table'])) { | ||
return new LegacySqlEntityMetadata($this->entityName); | ||
} | ||
throw new \CRM_Core_Exception("Unknown entity $this->entityName"); | ||
} | ||
return $this->meta; | ||
} | ||
|
||
private function getStorageProvider(): EntityStorageInterface { | ||
if (!isset($this->storage)) { | ||
$entity = EntityRepository::getEntity($this->entityName); | ||
if (isset($entity['storageProvider'])) { | ||
return new $entity['storageProvider']($this->entityName); | ||
} | ||
if (isset($entity['table'])) { | ||
return new SqlEntityStorage($this->entityName); | ||
} | ||
throw new \CRM_Core_Exception("Unknown entity $this->entityName"); | ||
} | ||
return $this->storage; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Civi\Schema; | ||
|
||
/** | ||
* Supports older extensions that created dao-based entities with | ||
* `civix generate:entity-boilerplate`. | ||
*/ | ||
class LegacySqlEntityMetadata extends EntityMetadataBase { | ||
|
||
/** | ||
* @return string|\CRM_Core_DAO | ||
*/ | ||
public function getClassName(): string { | ||
return $this->getEntity()['class']; | ||
} | ||
|
||
public function getProperty(string $propertyName) { | ||
switch ($propertyName) { | ||
case 'title': | ||
return $this->getClassName()::getEntityTitle(); | ||
|
||
case 'titlePlural': | ||
return $this->getClassName()::getEntityTitle(TRUE); | ||
|
||
case 'icon': | ||
return $this->getClassName()::$_icon ?? NULL; | ||
|
||
case 'paths': | ||
return $this->getClassName()::getEntityPaths(); | ||
|
||
case 'labelField': | ||
return $this->getClassName()::$_labelField; | ||
|
||
case 'primaryKey': | ||
return $this->getClassName()::$_primaryKey ?? ['id']; | ||
|
||
case 'description': | ||
return $this->getClassName()::getEntityDescription(); | ||
|
||
default: | ||
return $this->getEntity()[$propertyName] ?? NULL; | ||
} | ||
} | ||
|
||
public function getFields(): array { | ||
return $this->getClassName()::fields(); | ||
} | ||
|
||
public function getOptions(string $fieldName, array $values = NULL): ?array { | ||
// TODO: Implement getOptions() method. | ||
} | ||
|
||
} |
Oops, something went wrong.