123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- /*
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the MIT license. For more information, see
- * <http://www.doctrine-project.org>.
- */
- namespace Doctrine\ORM\Tools;
- use Doctrine\DBAL\Types\Type;
- use Doctrine\Inflector\InflectorFactory;
- use Doctrine\ORM\Mapping\ClassMetadataInfo;
- use Symfony\Component\Yaml\Yaml;
- use function array_merge;
- use function count;
- use function explode;
- use function file_get_contents;
- use function glob;
- use function in_array;
- use function is_array;
- use function is_dir;
- use function is_string;
- use function preg_match;
- use function strtolower;
- /**
- * Class to help with converting Doctrine 1 schema files to Doctrine 2 mapping files
- *
- * @link www.doctrine-project.org
- */
- class ConvertDoctrine1Schema
- {
- /** @var mixed[] */
- private $from;
- /** @var array<string,string> */
- private $legacyTypeMap = [
- // TODO: This list may need to be updated
- 'clob' => 'text',
- 'timestamp' => 'datetime',
- 'enum' => 'string',
- ];
- /**
- * Constructor passes the directory or array of directories
- * to convert the Doctrine 1 schema files from.
- *
- * @psalm-param list<string>|string $from
- */
- public function __construct($from)
- {
- $this->from = (array) $from;
- }
- /**
- * Gets an array of ClassMetadataInfo instances from the passed
- * Doctrine 1 schema.
- *
- * @return ClassMetadataInfo[] An array of ClassMetadataInfo instances
- *
- * @psalm-return list<ClassMetadataInfo>
- */
- public function getMetadata()
- {
- $schema = [];
- foreach ($this->from as $path) {
- if (is_dir($path)) {
- $files = glob($path . '/*.yml');
- foreach ($files as $file) {
- $schema = array_merge($schema, (array) Yaml::parse(file_get_contents($file)));
- }
- } else {
- $schema = array_merge($schema, (array) Yaml::parse(file_get_contents($path)));
- }
- }
- $metadatas = [];
- foreach ($schema as $className => $mappingInformation) {
- $metadatas[] = $this->convertToClassMetadataInfo($className, $mappingInformation);
- }
- return $metadatas;
- }
- /**
- * @param mixed[] $mappingInformation
- */
- private function convertToClassMetadataInfo(string $className, $mappingInformation): ClassMetadataInfo
- {
- $metadata = new ClassMetadataInfo($className);
- $this->convertTableName($className, $mappingInformation, $metadata);
- $this->convertColumns($className, $mappingInformation, $metadata);
- $this->convertIndexes($className, $mappingInformation, $metadata);
- $this->convertRelations($className, $mappingInformation, $metadata);
- return $metadata;
- }
- /**
- * @param mixed[] $model
- */
- private function convertTableName(string $className, array $model, ClassMetadataInfo $metadata): void
- {
- if (isset($model['tableName']) && $model['tableName']) {
- $e = explode('.', $model['tableName']);
- if (count($e) > 1) {
- $metadata->table['schema'] = $e[0];
- $metadata->table['name'] = $e[1];
- } else {
- $metadata->table['name'] = $e[0];
- }
- }
- }
- /**
- * @param string $className
- * @param mixed[] $model
- *
- * @return void
- */
- private function convertColumns($className, array $model, ClassMetadataInfo $metadata)
- {
- $id = false;
- if (isset($model['columns']) && $model['columns']) {
- foreach ($model['columns'] as $name => $column) {
- $fieldMapping = $this->convertColumn($className, $name, $column, $metadata);
- if (isset($fieldMapping['id']) && $fieldMapping['id']) {
- $id = true;
- }
- }
- }
- if (! $id) {
- $fieldMapping = [
- 'fieldName' => 'id',
- 'columnName' => 'id',
- 'type' => 'integer',
- 'id' => true,
- ];
- $metadata->mapField($fieldMapping);
- $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
- }
- }
- /**
- * @param string $className
- * @param string $name
- * @param string|mixed[] $column
- *
- * @return mixed[]
- *
- * @throws ToolsException
- */
- private function convertColumn($className, $name, $column, ClassMetadataInfo $metadata)
- {
- if (is_string($column)) {
- $string = $column;
- $column = [];
- $column['type'] = $string;
- }
- if (! isset($column['name'])) {
- $column['name'] = $name;
- }
- // check if a column alias was used (column_name as field_name)
- if (preg_match('/(\w+)\sas\s(\w+)/i', $column['name'], $matches)) {
- $name = $matches[1];
- $column['name'] = $name;
- $column['alias'] = $matches[2];
- }
- if (preg_match('/([a-zA-Z]+)\(([0-9]+)\)/', $column['type'], $matches)) {
- $column['type'] = $matches[1];
- $column['length'] = $matches[2];
- }
- $column['type'] = strtolower($column['type']);
- // check if legacy column type (1.x) needs to be mapped to a 2.0 one
- if (isset($this->legacyTypeMap[$column['type']])) {
- $column['type'] = $this->legacyTypeMap[$column['type']];
- }
- if (! Type::hasType($column['type'])) {
- throw ToolsException::couldNotMapDoctrine1Type($column['type']);
- }
- $fieldMapping = [];
- if (isset($column['primary'])) {
- $fieldMapping['id'] = true;
- }
- $fieldMapping['fieldName'] = $column['alias'] ?? $name;
- $fieldMapping['columnName'] = $column['name'];
- $fieldMapping['type'] = $column['type'];
- if (isset($column['length'])) {
- $fieldMapping['length'] = $column['length'];
- }
- $allowed = ['precision', 'scale', 'unique', 'options', 'notnull', 'version'];
- foreach ($column as $key => $value) {
- if (in_array($key, $allowed)) {
- $fieldMapping[$key] = $value;
- }
- }
- $metadata->mapField($fieldMapping);
- if (isset($column['autoincrement'])) {
- $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
- } elseif (isset($column['sequence'])) {
- $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
- $definition = [
- 'sequenceName' => is_array($column['sequence']) ? $column['sequence']['name'] : $column['sequence'],
- ];
- if (isset($column['sequence']['size'])) {
- $definition['allocationSize'] = $column['sequence']['size'];
- }
- if (isset($column['sequence']['value'])) {
- $definition['initialValue'] = $column['sequence']['value'];
- }
- $metadata->setSequenceGeneratorDefinition($definition);
- }
- return $fieldMapping;
- }
- /**
- * @param string $className
- * @param mixed[] $model
- *
- * @return void
- */
- private function convertIndexes($className, array $model, ClassMetadataInfo $metadata)
- {
- if (empty($model['indexes'])) {
- return;
- }
- foreach ($model['indexes'] as $name => $index) {
- $type = isset($index['type']) && $index['type'] === 'unique'
- ? 'uniqueConstraints' : 'indexes';
- $metadata->table[$type][$name] = [
- 'columns' => $index['fields'],
- ];
- }
- }
- /**
- * @param string $className
- * @param mixed[] $model
- *
- * @return void
- */
- private function convertRelations($className, array $model, ClassMetadataInfo $metadata)
- {
- if (empty($model['relations'])) {
- return;
- }
- $inflector = InflectorFactory::create()->build();
- foreach ($model['relations'] as $name => $relation) {
- if (! isset($relation['alias'])) {
- $relation['alias'] = $name;
- }
- if (! isset($relation['class'])) {
- $relation['class'] = $name;
- }
- if (! isset($relation['local'])) {
- $relation['local'] = $inflector->tableize($relation['class']);
- }
- if (! isset($relation['foreign'])) {
- $relation['foreign'] = 'id';
- }
- if (! isset($relation['foreignAlias'])) {
- $relation['foreignAlias'] = $className;
- }
- if (isset($relation['refClass'])) {
- $type = 'many';
- $foreignType = 'many';
- $joinColumns = [];
- } else {
- $type = $relation['type'] ?? 'one';
- $foreignType = $relation['foreignType'] ?? 'many';
- $joinColumns = [
- [
- 'name' => $relation['local'],
- 'referencedColumnName' => $relation['foreign'],
- 'onDelete' => $relation['onDelete'] ?? null,
- ],
- ];
- }
- if ($type === 'one' && $foreignType === 'one') {
- $method = 'mapOneToOne';
- } elseif ($type === 'many' && $foreignType === 'many') {
- $method = 'mapManyToMany';
- } else {
- $method = 'mapOneToMany';
- }
- $associationMapping = [];
- $associationMapping['fieldName'] = $relation['alias'];
- $associationMapping['targetEntity'] = $relation['class'];
- $associationMapping['mappedBy'] = $relation['foreignAlias'];
- $associationMapping['joinColumns'] = $joinColumns;
- $metadata->$method($associationMapping);
- }
- }
- }
|