![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/Framework/Setup/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ namespace Ecombricks\Framework\Setup; /** * Schema setup trait */ trait SchemaSetupTrait { /** * Setup * * @var \Magento\Framework\Setup\SchemaSetupInterface */ protected $setup; /** * Set setup * * @param \Magento\Framework\Setup\SchemaSetupInterface $setup * @return $this */ protected function setSetup(\Magento\Framework\Setup\SchemaSetupInterface $setup) { $this->setup = $setup; return $this; } /** * Get setup * * @return \Magento\Framework\Setup\SchemaSetupInterface */ protected function getSetup() { if (empty($this->setup) || !($this->setup instanceof \Magento\Framework\Setup\SchemaSetupInterface)) { throw new \Magento\Framework\Exception\LocalizedException(__('Setup is undefined for the schema installer.')); } return $this->setup; } /** * Get connection * * @return \Magento\Framework\DB\Adapter\AdapterInterface */ protected function getConnection() { return $this->getSetup()->getConnection(); } /** * Get table * * @param string $tableName * @return string */ protected function getTable($tableName) { return $this->getSetup()->getTable($tableName); } /** * Create table * * @param \Magento\Framework\DB\Ddl\Table $table * @return $this */ protected function createTable(\Magento\Framework\DB\Ddl\Table $table) { $connection = $this->getConnection(); if ($connection->isTableExists($table->getName())) { return $this; } $connection->createTable($table); return $this; } /** * Drop table * * @param string $tableName * @return $this */ protected function dropTable($tableName) { $connection = $this->getConnection(); if (!$connection->isTableExists($this->getTable($tableName))) { return $this; } $connection->dropTable($this->getTable($tableName)); return $this; } /** * Create column * * @param string $tableName * @param string $columnName * @param array $definition * @return $this */ protected function createColumn($tableName, $columnName, $definition) { $connection = $this->getConnection(); if ($connection->tableColumnExists($this->getTable($tableName), $columnName)) { return $this; } $connection->addColumn($this->getTable($tableName), $columnName, $definition); return $this; } /** * Drop column * * @param string $tableName * @param string $columnName * @return $this */ protected function dropColumn($tableName, $columnName) { $connection = $this->getConnection(); if (!$connection->tableColumnExists($this->getTable($tableName), $columnName)) { return $this; } foreach ($connection->getForeignKeys($this->getTable($tableName)) as $foreignKey) { if ($foreignKey['COLUMN_NAME'] !== $columnName) { continue; } $connection->dropForeignKey($this->getTable($tableName), $foreignKey['FK_NAME']); } $connection->dropColumn($this->getTable($tableName), $columnName); return $this; } /** * Describe table * * @param string $tableName * @return array */ protected function describeTable($tableName) { return $this->getConnection()->describeTable($this->getTable($tableName)); } /** * Get columns * * @param string $tableName * @return array */ protected function getColumns($tableName) { $connection = $this->getConnection(); $columns = []; foreach ($this->describeTable($tableName) as $columnDdl) { $columns[] = $connection->getColumnCreateByDescribe($columnDdl); } return $columns; } /** * Add table column * * @param \Magento\Framework\DB\Ddl\Table $table * @param array $column * @return $this */ protected function addTableColumn($table, $column) { $table->addColumn( $column['name'], $column['type'], $column['length'] ?? null, $column['options'], $column['comment'] ); return $this; } /** * Add table index * * @param \Magento\Framework\DB\Ddl\Table $table * @param string $tableName * @param array $columns * @param string $type * @return $this */ protected function addTableIndex($table, $tableName, $columns, $type) { $table->addIndex( $this->getConnection()->getIndexName($tableName, $columns, $type), $columns, ['type' => $type] ); return $this; } /** * Get DDL action * * @param string $action * @return string */ protected function getDdlAction($action) { switch ($action) { case \Magento\Framework\DB\Adapter\AdapterInterface::FK_ACTION_CASCADE: return \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE; case \Magento\Framework\DB\Adapter\AdapterInterface::FK_ACTION_SET_NULL: return \Magento\Framework\DB\Ddl\Table::ACTION_SET_NULL; case \Magento\Framework\DB\Adapter\AdapterInterface::FK_ACTION_RESTRICT: return \Magento\Framework\DB\Ddl\Table::ACTION_RESTRICT; default: return \Magento\Framework\DB\Ddl\Table::ACTION_NO_ACTION; } } /** * Add table foreign key * * @param \Magento\Framework\DB\Ddl\Table $table * @param string $tableName * @param string $columnName * @param string $referenceTableName * @param string $referenceColumnName * @param string $onDelete * @return $this */ protected function addTableForeignKey($table, $tableName, $columnName, $referenceTableName, $referenceColumnName, $onDelete) { $connection = $this->getConnection(); $foreignKeyName = $connection->getForeignKeyName( $tableName, $columnName, $referenceTableName, $referenceColumnName ); $table->addForeignKey( $foreignKeyName, $columnName, $this->getTable($referenceTableName), $referenceColumnName, $this->getDdlAction($onDelete) ); return $this; } }