master

laravel/framework

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

TableGuesser.php

TLDR

This file defines a class called TableGuesser in the Illuminate\Database\Console\Migrations namespace. The class has a static method called guess which is used to guess the table name and "creation" status of a migration.

Methods

guess

The guess method attempts to guess the table name and "creation" status of the given migration. It takes a string parameter $migration and returns an array.

Classes

Class TableGuesser

The TableGuesser class provides a method to guess the table name and "creation" status of a migration.

<?php

namespace Illuminate\Database\Console\Migrations;

class TableGuesser
{
    const CREATE_PATTERNS = [
        '/^create_(\w+)_table$/',
        '/^create_(\w+)$/',
    ];

    const CHANGE_PATTERNS = [
        '/.+_(to|from|in)_(\w+)_table$/',
        '/.+_(to|from|in)_(\w+)$/',
    ];

    /**
     * Attempt to guess the table name and "creation" status of the given migration.
     *
     * @param  string  $migration
     * @return array
     */
    public static function guess($migration)
    {
        foreach (self::CREATE_PATTERNS as $pattern) {
            if (preg_match($pattern, $migration, $matches)) {
                return [$matches[1], $create = true];
            }
        }

        foreach (self::CHANGE_PATTERNS as $pattern) {
            if (preg_match($pattern, $migration, $matches)) {
                return [$matches[2], $create = false];
            }
        }
    }
}