Contao\SqlFileParser::parse PHP Method

parse() public static method

Parse a database.sql file
public static parse ( string $file ) : array
$file string The file path
return array An array of DCA table settings
    public static function parse($file)
    {
        if (!file_exists($file)) {
            throw new \InvalidArgumentException('Invalid file ' . $file);
        }
        $table = '';
        $return = array();
        $data = file($file);
        foreach ($data as $k => $v) {
            $key_name = array();
            $subpatterns = array();
            // Unset comments and empty lines
            if (preg_match('/^[#-]+/', $v) || !strlen(trim($v))) {
                unset($data[$k]);
                continue;
            }
            // Store the table names
            if (preg_match('/^CREATE TABLE `([^`]+)`/i', $v, $subpatterns)) {
                $table = $subpatterns[1];
            } elseif ($table != '' && preg_match('/^\\)([^;]+);/', $v, $subpatterns)) {
                $return[$table]['TABLE_OPTIONS'] = $subpatterns[1];
                $table = '';
            } elseif ($table != '') {
                preg_match('/^[^`]*`([^`]+)`/', trim($v), $key_name);
                $first = preg_replace('/\\s[^\\n\\r]+/', '', $key_name[0]);
                $key = $key_name[1];
                // Create definitions
                if (in_array($first, array('KEY', 'PRIMARY', 'PRIMARY KEY', 'FOREIGN', 'FOREIGN KEY', 'INDEX', 'UNIQUE', 'FULLTEXT', 'CHECK'))) {
                    if (strncmp($first, 'PRIMARY', 7) === 0) {
                        $key = 'PRIMARY';
                    }
                    $return[$table]['TABLE_CREATE_DEFINITIONS'][$key] = preg_replace('/,$/', '', trim($v));
                } else {
                    $return[$table]['TABLE_FIELDS'][$key] = preg_replace('/,$/', '', trim($v));
                }
            }
        }
        return $return;
    }

Usage Example

 /**
  * Returns the target database structure from the database.sql files.
  *
  * @return array An array of tables and fields
  */
 private function getFromFile()
 {
     $return = [];
     /** @var SplFileInfo[] $files */
     $files = $this->finder->findIn('config')->depth(0)->files()->name('database.sql');
     foreach ($files as $file) {
         $return = array_merge_recursive($return, SqlFileParser::parse($file));
     }
     ksort($return);
     return $return;
 }
All Usage Examples Of Contao\SqlFileParser::parse
SqlFileParser