From df7a2ee0a91190e64933c961fdd024e253df861b Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 30 Oct 2015 15:34:19 +0100 Subject: [PATCH] Repository: Add native support for virtual table names --- library/Icinga/Repository/Repository.php | 43 +++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Repository/Repository.php b/library/Icinga/Repository/Repository.php index 40ace5c01..091b286bb 100644 --- a/library/Icinga/Repository/Repository.php +++ b/library/Icinga/Repository/Repository.php @@ -52,6 +52,16 @@ abstract class Repository implements Selectable */ protected $baseTable; + /** + * The virtual tables being provided + * + * This may be initialized by concrete repository implementations with an array + * where a key is the name of a virtual table and its value the real table name. + * + * @var array + */ + protected $virtualTables; + /** * The query columns being provided * @@ -255,6 +265,32 @@ abstract class Repository implements Selectable return $this->baseTable; } + /** + * Return the virtual tables being provided + * + * Calls $this->initializeVirtualTables() in case $this->virtualTables is null. + * + * @return array + */ + public function getVirtualTables() + { + if ($this->virtualTables === null) { + $this->virtualTables = $this->initializeVirtualTables(); + } + + return $this->virtualTables; + } + + /** + * Overwrite this in your repository implementation in case you need to initialize the virtual tables lazily + * + * @return array + */ + protected function initializeVirtualTables() + { + return array(); + } + /** * Return the query columns being provided * @@ -792,7 +828,7 @@ abstract class Repository implements Selectable } /** - * Validate that the requested table exists + * Validate that the requested table exists and resolve it's real name if necessary * * @param string $table The table to validate * @param RepositoryQuery $query An optional query to pass as context @@ -809,6 +845,11 @@ abstract class Repository implements Selectable throw new ProgrammingError('Table "%s" not found', $table); } + $virtualTables = $this->getVirtualTables(); + if (isset($virtualTables[$table])) { + $table = $virtualTables[$table]; + } + return $table; }