Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformat project according to PSR-2 coding standard #147

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reformat PHP files according to PSR-2 standard
  • Loading branch information
akalongman committed Oct 5, 2017
commit 9418a3f8aaeb0a36f7a9127b644fd329f75863a3
502 changes: 266 additions & 236 deletions src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php

Large diffs are not rendered by default.

137 changes: 73 additions & 64 deletions src/Xethron/MigrationsGenerator/Generators/ForeignKeyGenerator.php
Original file line number Diff line number Diff line change
@@ -1,75 +1,84 @@
<?php namespace Xethron\MigrationsGenerator\Generators;
<?php

class ForeignKeyGenerator {
namespace Xethron\MigrationsGenerator\Generators;

/**
* @var string
*/
protected $table;
class ForeignKeyGenerator
{

/**
* Get array of foreign keys
*
* @param string $table Table Name
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
* @param $ignoreForeignKeyNames
*
* @return array
*/
public function generate($table, $schema, $ignoreForeignKeyNames)
{
$this->table = $table;
$fields = [];
/**
* @var string
*/
protected $table;

$foreignKeys = $schema->listTableForeignKeys($table);
/**
* Get array of foreign keys
*
* @param string $table Table Name
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
* @param $ignoreForeignKeyNames
*
* @return array
*/
public function generate($table, $schema, $ignoreForeignKeyNames)
{
$this->table = $table;
$fields = [];

if ( empty( $foreignKeys ) ) return array();
$foreignKeys = $schema->listTableForeignKeys($table);

foreach ( $foreignKeys as $foreignKey ) {
$fields[] = [
'name' => $this->getName($foreignKey, $ignoreForeignKeyNames),
'field' => $foreignKey->getLocalColumns()[0],
'references' => $foreignKey->getForeignColumns()[0],
'on' => $foreignKey->getForeignTableName(),
'onUpdate' => $foreignKey->hasOption('onUpdate') ? $foreignKey->getOption('onUpdate') : 'RESTRICT',
'onDelete' => $foreignKey->hasOption('onDelete') ? $foreignKey->getOption('onDelete') : 'RESTRICT',
];
}
return $fields;
}
if (empty($foreignKeys)) {
return [];
}

/**
* @param $foreignKey
* @param bool $ignoreForeignKeyNames
*
* @return null
*/
private function getName($foreignKey, $ignoreForeignKeyNames) {
if ($ignoreForeignKeyNames or $this->isDefaultName($foreignKey)) {
return null;
}
return $foreignKey->getName();
}
foreach ($foreignKeys as $foreignKey) {
$fields[] = [
'name' => $this->getName($foreignKey, $ignoreForeignKeyNames),
'field' => $foreignKey->getLocalColumns()[0],
'references' => $foreignKey->getForeignColumns()[0],
'on' => $foreignKey->getForeignTableName(),
'onUpdate' => $foreignKey->hasOption('onUpdate') ? $foreignKey->getOption('onUpdate') : 'RESTRICT',
'onDelete' => $foreignKey->hasOption('onDelete') ? $foreignKey->getOption('onDelete') : 'RESTRICT',
];
}

/**
* @param $foreignKey
*
* @return bool
*/
private function isDefaultName($foreignKey) {
return $foreignKey->getName() === $this->createIndexName($foreignKey->getLocalColumns()[0]);
}
return $fields;
}

/**
* Create a default index name for the table.
*
* @param string $column
* @return string
*/
protected function createIndexName($column)
{
$index = strtolower($this->table.'_'.$column.'_foreign');
/**
* @param $foreignKey
* @param bool $ignoreForeignKeyNames
*
* @return null
*/
private function getName($foreignKey, $ignoreForeignKeyNames)
{
if ($ignoreForeignKeyNames or $this->isDefaultName($foreignKey)) {
return null;
}

return str_replace(array('-', '.'), '_', $index);
}
return $foreignKey->getName();
}

/**
* @param $foreignKey
*
* @return bool
*/
private function isDefaultName($foreignKey)
{
return $foreignKey->getName() === $this->createIndexName($foreignKey->getLocalColumns()[0]);
}

/**
* Create a default index name for the table.
*
* @param string $column
* @return string
*/
protected function createIndexName($column)
{
$index = strtolower($this->table . '_' . $column . '_foreign');

return str_replace(['-', '.'], '_', $index);
}
}
240 changes: 123 additions & 117 deletions src/Xethron/MigrationsGenerator/Generators/IndexGenerator.php
Original file line number Diff line number Diff line change
@@ -1,119 +1,125 @@
<?php namespace Xethron\MigrationsGenerator\Generators;


class IndexGenerator {

/**
* @var array
*/
protected $indexes;

/**
* @var array
*/
protected $multiFieldIndexes;

/**
* @var bool
*/
private $ignoreIndexNames;

/**
* @param string $table Table Name
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
* @param bool $ignoreIndexNames
*/
public function __construct($table, $schema, $ignoreIndexNames)
{
$this->indexes = array();
$this->multiFieldIndexes = array();
$this->ignoreIndexNames = $ignoreIndexNames;

$indexes = $schema->listTableIndexes( $table );

foreach ( $indexes as $index ) {
$indexArray = $this->indexToArray($table, $index);
if ( count( $indexArray['columns'] ) == 1 ) {
$columnName = $indexArray['columns'][0];
$this->indexes[ $columnName ] = (object) $indexArray;
} else {
$this->multiFieldIndexes[] = (object) $indexArray;
}
}
}


/**
* @param string $table
* @param \Doctrine\DBAL\Schema\Index $index
* @return array
*/
protected function indexToArray($table, $index)
{
if ( $index->isPrimary() ) {
$type = 'primary';
} elseif ( $index->isUnique() ) {
$type = 'unique';
} else {
$type = 'index';
}
$array = ['type' => $type, 'name' => null, 'columns' => $index->getColumns()];

if ( ! $this->ignoreIndexNames and ! $this->isDefaultIndexName($table, $index->getName(), $type, $index->getColumns())) {
// Sent Index name to exclude spaces
$array['name'] = str_replace(' ', '', $index->getName());
}
return $array;
}

/**
* @param string $table Table Name
* @param string $type Index Type
* @param string|array $columns Column Names
* @return string
*/
protected function getDefaultIndexName( $table, $type, $columns )
{
if ($type=='primary') {
return 'PRIMARY';
}
if ( is_array( $columns ) ) {
$columns = implode( '_', $columns );
}
return $table .'_'. $columns .'_'. $type;
}

/**
* @param string $table Table Name
* @param string $name Current Name
* @param string $type Index Type
* @param string|array $columns Column Names
* @return bool
*/
protected function isDefaultIndexName( $table, $name, $type, $columns )
{
return $name == $this->getDefaultIndexName( $table, $type, $columns );
}


/**
* @param string $name
* @return null|object
*/
public function getIndex($name)
{
if ( isset( $this->indexes[ $name ] ) ) {
return (object) $this->indexes[ $name ];
}
return null;
}

/**
* @return null|object
*/
public function getMultiFieldIndexes()
{
return $this->multiFieldIndexes;
}
<?php

namespace Xethron\MigrationsGenerator\Generators;


class IndexGenerator
{

/**
* @var array
*/
protected $indexes;

/**
* @var array
*/
protected $multiFieldIndexes;

/**
* @var bool
*/
private $ignoreIndexNames;

/**
* @param string $table Table Name
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
* @param bool $ignoreIndexNames
*/
public function __construct($table, $schema, $ignoreIndexNames)
{
$this->indexes = [];
$this->multiFieldIndexes = [];
$this->ignoreIndexNames = $ignoreIndexNames;

$indexes = $schema->listTableIndexes($table);

foreach ($indexes as $index) {
$indexArray = $this->indexToArray($table, $index);
if (count($indexArray['columns']) == 1) {
$columnName = $indexArray['columns'][0];
$this->indexes[$columnName] = (object) $indexArray;
} else {
$this->multiFieldIndexes[] = (object) $indexArray;
}
}
}


/**
* @param string $table
* @param \Doctrine\DBAL\Schema\Index $index
* @return array
*/
protected function indexToArray($table, $index)
{
if ($index->isPrimary()) {
$type = 'primary';
} elseif ($index->isUnique()) {
$type = 'unique';
} else {
$type = 'index';
}
$array = ['type' => $type, 'name' => null, 'columns' => $index->getColumns()];

if (! $this->ignoreIndexNames and ! $this->isDefaultIndexName($table, $index->getName(), $type, $index->getColumns())) {
// Sent Index name to exclude spaces
$array['name'] = str_replace(' ', '', $index->getName());
}

return $array;
}

/**
* @param string $table Table Name
* @param string $type Index Type
* @param string|array $columns Column Names
* @return string
*/
protected function getDefaultIndexName($table, $type, $columns)
{
if ($type == 'primary') {
return 'PRIMARY';
}
if (is_array($columns)) {
$columns = implode('_', $columns);
}

return $table . '_' . $columns . '_' . $type;
}

/**
* @param string $table Table Name
* @param string $name Current Name
* @param string $type Index Type
* @param string|array $columns Column Names
* @return bool
*/
protected function isDefaultIndexName($table, $name, $type, $columns)
{
return $name == $this->getDefaultIndexName($table, $type, $columns);
}


/**
* @param string $name
* @return null|object
*/
public function getIndex($name)
{
if (isset($this->indexes[$name])) {
return (object) $this->indexes[$name];
}

return null;
}

/**
* @return null|object
*/
public function getMultiFieldIndexes()
{
return $this->multiFieldIndexes;
}

}
Loading