diff --git a/Command/DiffFileCommand.php b/Command/DiffFileCommand.php index 9d4bab9..8860923 100644 --- a/Command/DiffFileCommand.php +++ b/Command/DiffFileCommand.php @@ -121,13 +121,45 @@ protected function saveCurrentSchema($configuration, $schema, $version) file_put_contents($filepath, serialize($schema)); } + /** + * + * @return type + */ + protected function getSchemaProvider() + { + if (!$this->schemaProvider) { + $this->schemaProvider = new OrmSchemaProvider($this->getHelper('entityManager')->getEntityManager()); + } + + return $this->schemaProvider; + } + + /** + * Resolve a table name from its fully qualified name. The `$name` argument + * comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return + * a namespaced name with the form `{namespace}.{tableName}`. This extracts + * the table name from that. + * + * @param string $name + * @return string + */ + protected function resolveTableName($name) + { + $pos = strpos($name, '.'); + + return false === $pos ? $name : substr($name, $pos + 1); + } + /** * * @param Configuration $configuration * @param array $sql + * @param type $formatted + * @param type $lineLength * @return type + * @throws \InvalidArgumentException */ - protected function buildCodeFromSql(Configuration $configuration, array $sql) + private function buildCodeFromSql(Configuration $configuration, array $sql, $formatted = false, $lineLength = 120) { $currentPlatform = $configuration->getConnection()->getDatabasePlatform()->getName(); $code = []; @@ -135,6 +167,21 @@ protected function buildCodeFromSql(Configuration $configuration, array $sql) if (stripos($query, $configuration->getMigrationsTableName()) !== false) { continue; } + + if ($formatted) { + if (!class_exists('\SqlFormatter')) { + throw new \InvalidArgumentException( + 'The "--formatted" option can only be used if the sql formatter is installed.'.'Please run "composer require jdorn/sql-formatter".' + ); + } + + $maxLength = $lineLength - 18 - 8; // max - php code length - indentation + + if (strlen($query) > $maxLength) { + $query = \SqlFormatter::format($query, false); + } + } + $code[] = sprintf("\$this->addSql(%s);", var_export($query, true)); } @@ -152,33 +199,4 @@ protected function buildCodeFromSql(Configuration $configuration, array $sql) return implode("\n", $code); } - - /** - * - * @return type - */ - protected function getSchemaProvider() - { - if (!$this->schemaProvider) { - $this->schemaProvider = new OrmSchemaProvider($this->getHelper('entityManager')->getEntityManager()); - } - - return $this->schemaProvider; - } - - /** - * Resolve a table name from its fully qualified name. The `$name` argument - * comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return - * a namespaced name with the form `{namespace}.{tableName}`. This extracts - * the table name from that. - * - * @param string $name - * @return string - */ - protected function resolveTableName($name) - { - $pos = strpos($name, '.'); - - return false === $pos ? $name : substr($name, $pos + 1); - } } diff --git a/composer.json b/composer.json index b7b47a1..3aff580 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,6 @@ "php": ">=5.4.0", "symfony/framework-bundle": "~2.3|~3.0", "doctrine/doctrine-bundle": "~1.0", - "doctrine/migrations": "~1.0" + "doctrine/migrations": "~1.4" } }