master

laravel/framework

Last updated at: 29/12/2023 09:22

RenameColumn.php

TLDR

The provided file, RenameColumn.php, is a class within the Illuminate\Database\Schema\Grammars namespace. It contains a static method, compile, that compiles a rename column command for a database schema. The method takes in a Grammar instance, a Blueprint instance, a Fluent instance, and a Connection instance as parameters and returns an array.

Methods

compile

This method compiles a rename column command for a database schema.

  • Parameters:
    • $grammar (Illuminate\Database\Schema\Grammars\Grammar): The grammar instance for the rename column command.
    • $blueprint (Illuminate\Database\Schema\Blueprint): The blueprint instance for the rename column command.
    • $command (Illuminate\Support\Fluent): The command instance for the rename column command.
    • $connection (Illuminate\Database\Connection): The connection instance for the rename column command.
  • Return Value: An array representing the SQL statement for the rename column command.

getRenamedDiff

This is a protected method that retrieves a new column instance with the new column name.

  • Parameters:
    • $grammar (Illuminate\Database\Schema\Grammars\Grammar): The grammar instance for the rename column command.
    • $blueprint (Illuminate\Database\Schema\Blueprint): The blueprint instance for the rename column command.
    • $command (Illuminate\Support\Fluent): The command instance for the rename column command.
    • $column (Doctrine\DBAL\Schema\Column): The column instance for the rename column command.
    • $schema (Doctrine\DBAL\Schema\AbstractSchemaManager): The schema instance for the rename column command.
  • Return Value: A Doctrine\DBAL\Schema\TableDiff instance representing the renamed column.

setRenamedColumns

This is a protected method that sets the renamed columns on the table diff.

  • Parameters:
    • $tableDiff (Doctrine\DBAL\Schema\TableDiff): The table diff instance for the rename column command.
    • $command (Illuminate\Support\Fluent): The command instance for the rename column command.
    • $column (Doctrine\DBAL\Schema\Column): The column instance for the rename column command.
  • Return Value: A Doctrine\DBAL\Schema\TableDiff instance with the renamed columns set.

getWritableColumnOptions

This is a private method that retrieves the writable column options.

  • Parameters:
    • $column (Doctrine\DBAL\Schema\Column): The column instance for the rename column command.
  • Return Value: An array representing the writable column options.
<?php

namespace Illuminate\Database\Schema\Grammars;

use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\TableDiff;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Fluent;

class RenameColumn
{
    /**
     * Compile a rename column command.
     *
     * @param  \Illuminate\Database\Schema\Grammars\Grammar  $grammar
     * @param  \Illuminate\Database\Schema\Blueprint  $blueprint
     * @param  \Illuminate\Support\Fluent  $command
     * @param  \Illuminate\Database\Connection  $connection
     * @return array
     */
    public static function compile(Grammar $grammar, Blueprint $blueprint, Fluent $command, Connection $connection)
    {
        $schema = $connection->getDoctrineSchemaManager();
        $databasePlatform = $connection->getDoctrineConnection()->getDatabasePlatform();
        $databasePlatform->registerDoctrineTypeMapping('enum', 'string');

        $column = $connection->getDoctrineColumn(
            $grammar->getTablePrefix().$blueprint->getTable(), $command->from
        );

        return (array) $databasePlatform->getAlterTableSQL(static::getRenamedDiff(
            $grammar, $blueprint, $command, $column, $schema
        ));
    }

    /**
     * Get a new column instance with the new column name.
     *
     * @param  \Illuminate\Database\Schema\Grammars\Grammar  $grammar
     * @param  \Illuminate\Database\Schema\Blueprint  $blueprint
     * @param  \Illuminate\Support\Fluent  $command
     * @param  \Doctrine\DBAL\Schema\Column  $column
     * @param  \Doctrine\DBAL\Schema\AbstractSchemaManager  $schema
     * @return \Doctrine\DBAL\Schema\TableDiff
     */
    protected static function getRenamedDiff(Grammar $grammar, Blueprint $blueprint, Fluent $command, Column $column, SchemaManager $schema)
    {
        return static::setRenamedColumns(
            $grammar->getDoctrineTableDiff($blueprint, $schema), $command, $column
        );
    }

    /**
     * Set the renamed columns on the table diff.
     *
     * @param  \Doctrine\DBAL\Schema\TableDiff  $tableDiff
     * @param  \Illuminate\Support\Fluent  $command
     * @param  \Doctrine\DBAL\Schema\Column  $column
     * @return \Doctrine\DBAL\Schema\TableDiff
     */
    protected static function setRenamedColumns(TableDiff $tableDiff, Fluent $command, Column $column)
    {
        return new TableDiff(
            $tableDiff->getOldTable(),
            $tableDiff->getAddedColumns(),
            $tableDiff->getModifiedColumns(),
            $tableDiff->getDroppedColumns(),
            [$command->from => new Column($command->to, $column->getType(), self::getWritableColumnOptions($column))],
            $tableDiff->getAddedIndexes(),
            $tableDiff->getModifiedIndexes(),
            $tableDiff->getDroppedIndexes(),
            $tableDiff->getRenamedIndexes(),
            $tableDiff->getAddedForeignKeys(),
            $tableDiff->getModifiedColumns(),
            $tableDiff->getDroppedForeignKeys(),
        );
    }

    /**
     * Get the writable column options.
     *
     * @param  \Doctrine\DBAL\Schema\Column  $column
     * @return array
     */
    private static function getWritableColumnOptions(Column $column)
    {
        return array_filter($column->toArray(), function (string $name) use ($column) {
            return method_exists($column, 'set'.$name);
        }, ARRAY_FILTER_USE_KEY);
    }
}