@@ -9,91 +9,91 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class MapIterator implements Iterator, \JsonSerializable |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var Iterator |
|
| 14 | - */ |
|
| 15 | - protected $iterator; |
|
| 12 | + /** |
|
| 13 | + * @var Iterator |
|
| 14 | + */ |
|
| 15 | + protected $iterator; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var callable Modifies the current item in iterator |
|
| 19 | - */ |
|
| 20 | - protected $callable; |
|
| 17 | + /** |
|
| 18 | + * @var callable Modifies the current item in iterator |
|
| 19 | + */ |
|
| 20 | + protected $callable; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @param $iterator Iterator|array |
|
| 24 | - * @param $callable callable This can have two parameters |
|
| 25 | - * |
|
| 26 | - * @throws TDBMException |
|
| 27 | - */ |
|
| 28 | - public function __construct($iterator, callable $callable) |
|
| 29 | - { |
|
| 30 | - if (is_array($iterator)) { |
|
| 31 | - $this->iterator = new \ArrayIterator($iterator); |
|
| 32 | - } elseif (!($iterator instanceof Iterator)) { |
|
| 33 | - throw new TDBMException('$iterator parameter must be an instance of Iterator'); |
|
| 34 | - } else { |
|
| 35 | - $this->iterator = $iterator; |
|
| 36 | - } |
|
| 22 | + /** |
|
| 23 | + * @param $iterator Iterator|array |
|
| 24 | + * @param $callable callable This can have two parameters |
|
| 25 | + * |
|
| 26 | + * @throws TDBMException |
|
| 27 | + */ |
|
| 28 | + public function __construct($iterator, callable $callable) |
|
| 29 | + { |
|
| 30 | + if (is_array($iterator)) { |
|
| 31 | + $this->iterator = new \ArrayIterator($iterator); |
|
| 32 | + } elseif (!($iterator instanceof Iterator)) { |
|
| 33 | + throw new TDBMException('$iterator parameter must be an instance of Iterator'); |
|
| 34 | + } else { |
|
| 35 | + $this->iterator = $iterator; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - if ($callable instanceof \Closure) { |
|
| 39 | - // make sure there's one argument |
|
| 40 | - $reflection = new \ReflectionObject($callable); |
|
| 41 | - if ($reflection->hasMethod('__invoke')) { |
|
| 42 | - $method = $reflection->getMethod('__invoke'); |
|
| 43 | - if ($method->getNumberOfParameters() !== 1) { |
|
| 44 | - throw new TDBMException('$callable must accept one and only one parameter.'); |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - } |
|
| 38 | + if ($callable instanceof \Closure) { |
|
| 39 | + // make sure there's one argument |
|
| 40 | + $reflection = new \ReflectionObject($callable); |
|
| 41 | + if ($reflection->hasMethod('__invoke')) { |
|
| 42 | + $method = $reflection->getMethod('__invoke'); |
|
| 43 | + if ($method->getNumberOfParameters() !== 1) { |
|
| 44 | + throw new TDBMException('$callable must accept one and only one parameter.'); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - $this->callable = $callable; |
|
| 50 | - } |
|
| 49 | + $this->callable = $callable; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Alters the current item with $this->callable and returns a new item. |
|
| 54 | - * Be careful with your types as we can't do static type checking here! |
|
| 55 | - * |
|
| 56 | - * @return mixed |
|
| 57 | - */ |
|
| 58 | - public function current() |
|
| 59 | - { |
|
| 60 | - $callable = $this->callable; |
|
| 52 | + /** |
|
| 53 | + * Alters the current item with $this->callable and returns a new item. |
|
| 54 | + * Be careful with your types as we can't do static type checking here! |
|
| 55 | + * |
|
| 56 | + * @return mixed |
|
| 57 | + */ |
|
| 58 | + public function current() |
|
| 59 | + { |
|
| 60 | + $callable = $this->callable; |
|
| 61 | 61 | |
| 62 | - return $callable($this->iterator->current()); |
|
| 63 | - } |
|
| 62 | + return $callable($this->iterator->current()); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - public function next() |
|
| 66 | - { |
|
| 67 | - $this->iterator->next(); |
|
| 68 | - } |
|
| 65 | + public function next() |
|
| 66 | + { |
|
| 67 | + $this->iterator->next(); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - public function key() |
|
| 71 | - { |
|
| 72 | - return $this->iterator->key(); |
|
| 73 | - } |
|
| 70 | + public function key() |
|
| 71 | + { |
|
| 72 | + return $this->iterator->key(); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - public function valid() |
|
| 76 | - { |
|
| 77 | - return $this->iterator->valid(); |
|
| 78 | - } |
|
| 75 | + public function valid() |
|
| 76 | + { |
|
| 77 | + return $this->iterator->valid(); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - public function rewind() |
|
| 81 | - { |
|
| 82 | - $this->iterator->rewind(); |
|
| 83 | - } |
|
| 80 | + public function rewind() |
|
| 81 | + { |
|
| 82 | + $this->iterator->rewind(); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Casts the iterator to a PHP array. |
|
| 87 | - * |
|
| 88 | - * @return array |
|
| 89 | - */ |
|
| 90 | - public function toArray() |
|
| 91 | - { |
|
| 92 | - return iterator_to_array($this); |
|
| 93 | - } |
|
| 85 | + /** |
|
| 86 | + * Casts the iterator to a PHP array. |
|
| 87 | + * |
|
| 88 | + * @return array |
|
| 89 | + */ |
|
| 90 | + public function toArray() |
|
| 91 | + { |
|
| 92 | + return iterator_to_array($this); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - public function jsonSerialize() |
|
| 96 | - { |
|
| 97 | - return $this->toArray(); |
|
| 98 | - } |
|
| 95 | + public function jsonSerialize() |
|
| 96 | + { |
|
| 97 | + return $this->toArray(); |
|
| 98 | + } |
|
| 99 | 99 | } |
@@ -14,136 +14,136 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class TDBMSchemaAnalyzer |
| 16 | 16 | { |
| 17 | - private $connection; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var Schema |
|
| 21 | - */ |
|
| 22 | - private $schema; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - private $cachePrefix; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var Cache |
|
| 31 | - */ |
|
| 32 | - private $cache; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var SchemaAnalyzer |
|
| 36 | - */ |
|
| 37 | - private $schemaAnalyzer; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param Connection $connection The DBAL DB connection to use |
|
| 41 | - * @param Cache $cache A cache service to be used |
|
| 42 | - * @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths... |
|
| 43 | - * Will be automatically created if not passed. |
|
| 44 | - */ |
|
| 45 | - public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer) |
|
| 46 | - { |
|
| 47 | - $this->connection = $connection; |
|
| 48 | - $this->cache = $cache; |
|
| 49 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Returns a unique ID for the current connection. Useful for namespacing cache entries in the current connection. |
|
| 54 | - * |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - public function getCachePrefix() |
|
| 58 | - { |
|
| 59 | - if ($this->cachePrefix === null) { |
|
| 60 | - $this->cachePrefix = hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName()); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - return $this->cachePrefix; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Returns the (cached) schema. |
|
| 68 | - * |
|
| 69 | - * @return Schema |
|
| 70 | - */ |
|
| 71 | - public function getSchema() |
|
| 72 | - { |
|
| 73 | - if ($this->schema === null) { |
|
| 74 | - $cacheKey = $this->getCachePrefix().'_schema'; |
|
| 75 | - if ($this->cache->contains($cacheKey)) { |
|
| 76 | - $this->schema = $this->cache->fetch($cacheKey); |
|
| 77 | - } else { |
|
| 78 | - $this->schema = $this->connection->getSchemaManager()->createSchema(); |
|
| 79 | - $this->cache->save($cacheKey, $this->schema); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $this->schema; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Returns the list of pivot tables linked to table $tableName. |
|
| 88 | - * |
|
| 89 | - * @param string $tableName |
|
| 90 | - * |
|
| 91 | - * @return array|string[] |
|
| 92 | - */ |
|
| 93 | - public function getPivotTableLinkedToTable($tableName) |
|
| 94 | - { |
|
| 95 | - $cacheKey = $this->getCachePrefix().'_pivottables_link_'.$tableName; |
|
| 96 | - if ($this->cache->contains($cacheKey)) { |
|
| 97 | - return $this->cache->fetch($cacheKey); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $pivotTables = []; |
|
| 101 | - |
|
| 102 | - $junctionTables = $this->schemaAnalyzer->detectJunctionTables(true); |
|
| 103 | - foreach ($junctionTables as $table) { |
|
| 104 | - $fks = $table->getForeignKeys(); |
|
| 105 | - foreach ($fks as $fk) { |
|
| 106 | - if ($fk->getForeignTableName() == $tableName) { |
|
| 107 | - $pivotTables[] = $table->getName(); |
|
| 108 | - break; |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $this->cache->save($cacheKey, $pivotTables); |
|
| 114 | - |
|
| 115 | - return $pivotTables; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Returns the list of foreign keys pointing to the table represented by this bean, excluding foreign keys |
|
| 120 | - * from junction tables and from inheritance. |
|
| 121 | - * |
|
| 122 | - * @return ForeignKeyConstraint[] |
|
| 123 | - */ |
|
| 124 | - public function getIncomingForeignKeys($tableName) |
|
| 125 | - { |
|
| 126 | - $junctionTables = $this->schemaAnalyzer->detectJunctionTables(true); |
|
| 127 | - $junctionTableNames = array_map(function (Table $table) { return $table->getName(); }, $junctionTables); |
|
| 128 | - $childrenRelationships = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
| 129 | - |
|
| 130 | - $fks = []; |
|
| 131 | - foreach ($this->getSchema()->getTables() as $table) { |
|
| 132 | - foreach ($table->getForeignKeys() as $fk) { |
|
| 133 | - if ($fk->getForeignTableName() === $tableName) { |
|
| 134 | - if (in_array($fk->getLocalTableName(), $junctionTableNames)) { |
|
| 135 | - continue; |
|
| 136 | - } |
|
| 137 | - foreach ($childrenRelationships as $childFk) { |
|
| 138 | - if ($fk->getLocalTableName() === $childFk->getLocalTableName() && $fk->getLocalColumns() === $childFk->getLocalColumns()) { |
|
| 139 | - continue 2; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - $fks[] = $fk; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return $fks; |
|
| 148 | - } |
|
| 17 | + private $connection; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var Schema |
|
| 21 | + */ |
|
| 22 | + private $schema; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + private $cachePrefix; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var Cache |
|
| 31 | + */ |
|
| 32 | + private $cache; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var SchemaAnalyzer |
|
| 36 | + */ |
|
| 37 | + private $schemaAnalyzer; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param Connection $connection The DBAL DB connection to use |
|
| 41 | + * @param Cache $cache A cache service to be used |
|
| 42 | + * @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths... |
|
| 43 | + * Will be automatically created if not passed. |
|
| 44 | + */ |
|
| 45 | + public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer) |
|
| 46 | + { |
|
| 47 | + $this->connection = $connection; |
|
| 48 | + $this->cache = $cache; |
|
| 49 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Returns a unique ID for the current connection. Useful for namespacing cache entries in the current connection. |
|
| 54 | + * |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + public function getCachePrefix() |
|
| 58 | + { |
|
| 59 | + if ($this->cachePrefix === null) { |
|
| 60 | + $this->cachePrefix = hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName()); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + return $this->cachePrefix; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Returns the (cached) schema. |
|
| 68 | + * |
|
| 69 | + * @return Schema |
|
| 70 | + */ |
|
| 71 | + public function getSchema() |
|
| 72 | + { |
|
| 73 | + if ($this->schema === null) { |
|
| 74 | + $cacheKey = $this->getCachePrefix().'_schema'; |
|
| 75 | + if ($this->cache->contains($cacheKey)) { |
|
| 76 | + $this->schema = $this->cache->fetch($cacheKey); |
|
| 77 | + } else { |
|
| 78 | + $this->schema = $this->connection->getSchemaManager()->createSchema(); |
|
| 79 | + $this->cache->save($cacheKey, $this->schema); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + return $this->schema; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Returns the list of pivot tables linked to table $tableName. |
|
| 88 | + * |
|
| 89 | + * @param string $tableName |
|
| 90 | + * |
|
| 91 | + * @return array|string[] |
|
| 92 | + */ |
|
| 93 | + public function getPivotTableLinkedToTable($tableName) |
|
| 94 | + { |
|
| 95 | + $cacheKey = $this->getCachePrefix().'_pivottables_link_'.$tableName; |
|
| 96 | + if ($this->cache->contains($cacheKey)) { |
|
| 97 | + return $this->cache->fetch($cacheKey); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $pivotTables = []; |
|
| 101 | + |
|
| 102 | + $junctionTables = $this->schemaAnalyzer->detectJunctionTables(true); |
|
| 103 | + foreach ($junctionTables as $table) { |
|
| 104 | + $fks = $table->getForeignKeys(); |
|
| 105 | + foreach ($fks as $fk) { |
|
| 106 | + if ($fk->getForeignTableName() == $tableName) { |
|
| 107 | + $pivotTables[] = $table->getName(); |
|
| 108 | + break; |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $this->cache->save($cacheKey, $pivotTables); |
|
| 114 | + |
|
| 115 | + return $pivotTables; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Returns the list of foreign keys pointing to the table represented by this bean, excluding foreign keys |
|
| 120 | + * from junction tables and from inheritance. |
|
| 121 | + * |
|
| 122 | + * @return ForeignKeyConstraint[] |
|
| 123 | + */ |
|
| 124 | + public function getIncomingForeignKeys($tableName) |
|
| 125 | + { |
|
| 126 | + $junctionTables = $this->schemaAnalyzer->detectJunctionTables(true); |
|
| 127 | + $junctionTableNames = array_map(function (Table $table) { return $table->getName(); }, $junctionTables); |
|
| 128 | + $childrenRelationships = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
| 129 | + |
|
| 130 | + $fks = []; |
|
| 131 | + foreach ($this->getSchema()->getTables() as $table) { |
|
| 132 | + foreach ($table->getForeignKeys() as $fk) { |
|
| 133 | + if ($fk->getForeignTableName() === $tableName) { |
|
| 134 | + if (in_array($fk->getLocalTableName(), $junctionTableNames)) { |
|
| 135 | + continue; |
|
| 136 | + } |
|
| 137 | + foreach ($childrenRelationships as $childFk) { |
|
| 138 | + if ($fk->getLocalTableName() === $childFk->getLocalTableName() && $fk->getLocalColumns() === $childFk->getLocalColumns()) { |
|
| 139 | + continue 2; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + $fks[] = $fk; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return $fks; |
|
| 148 | + } |
|
| 149 | 149 | } |