Skip to content

Commit d520fe8

Browse files
authored
Merge pull request #1 from adeptofvoltron/master
First PR
2 parents 3392ca4 + c0d5eab commit d520fe8

20 files changed

+2809
-1
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
composer.phar
2+
/vendor/
3+
4+
tmp/
5+
*.tmp
6+
*.bak
7+
*~.nib
8+
9+
local.properties
10+
.settings/
11+
.loadpath
12+
.recommenders
13+
14+
phpunit.xml

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: php
2+
sudo: false
3+
php:
4+
- '7.1'
5+
- '7.0'
6+
- nightly
7+
8+
before_script:
9+
- composer self-update
10+
- composer install --prefer-dist --no-interaction
11+
12+
script:
13+
- ./vendor/bin/phpunit
14+
- ./vendor/bin/phpcs --standard=PSR2 ./src/ ./test/
15+
notifications:
16+
email: false

Api/ConfigBuilderInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PCF\MagentoMigration\Api;
6+
7+
interface ConfigBuilderInterface
8+
{
9+
10+
/**
11+
* @return string
12+
*/
13+
public function createConfigPath();
14+
}

Api/PathLocatorInterface.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PCF\MagentoMigration\Api;
6+
7+
interface PathLocatorInterface
8+
{
9+
const VENDOR_DIR_NAME = 'vendor';
10+
const MIGRATION_DIR_NAME = 'Migrations';
11+
12+
/**
13+
* @return string
14+
*/
15+
public function getPhinxBinaryPath();
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getVendorPath();
21+
22+
/**
23+
* @return array
24+
*/
25+
public function getAllMigrationDirs();
26+
}

CONTRIBUTING.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Contributing to Project
2+
Hi! :smiley_cat:
3+
Firstly, thanks for reading this doc.
4+
Everyone who programs in PHP and loves cats (and not only cats) can be a contributing member.
5+
This file contains only guidelines, not actual rules. Use your best judgment, and feel free to suggest changes to this document in a pull request.
6+
**Our only requirement is that all your code MUST follow psr standards and enable strict_types (php>=7).**
7+
8+
## Pull requests
9+
* Fork project and apply your changes
10+
* Create Pull Request to our repository
11+
* Include funny gif with cat in PR comm
12+
* Describe your changes in few sentences
13+
* Pat your cat and make him purrr :smirk_cat:
14+
15+
## Writing test
16+
Cats love getting new tests, so we love them to!! Remember improving code coverage makes your cat happy.
17+
18+
## Writing documentation
19+
Remember to add phpdoc to your code. We respect [phpDocumentator guides](https://www.phpdoc.org/docs/latest/glossary.html)

Command/Phinx.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PCF\MagentoMigration\Command;
6+
7+
use Psr\Log\LoggerInterface;
8+
use Symfony\Component\Console\Input\{InputArgument, InputOption};
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use PCF\MagentoMigration\Command\Phinx\CommandBuilder;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Process\Process;
14+
15+
class Phinx extends Command
16+
{
17+
const COMAND_CODE = 'pcf:migration';
18+
19+
/** @var CommandBuilder */
20+
private $commandBuilder;
21+
22+
/** @var Process */
23+
private $process;
24+
25+
/** @var LoggerInterface */
26+
private $logger;
27+
28+
/**
29+
* Phinx constructor.
30+
* @param CommandBuilder $commandBuilder
31+
* @param Process $process
32+
* @param LoggerInterface $logger
33+
*/
34+
public function __construct(CommandBuilder $commandBuilder, Process $process, LoggerInterface $logger)
35+
{
36+
parent::__construct();
37+
$this->commandBuilder = $commandBuilder;
38+
$this->process = $process;
39+
$this->logger = $logger;
40+
}
41+
42+
43+
protected function configure()
44+
{
45+
$this->addArgument('input', InputArgument::IS_ARRAY, 'Input command, same use as Phinx');
46+
$this->setName(self::COMAND_CODE)->setDescription('Migration using phinx command');
47+
}
48+
49+
/**
50+
* @param InputInterface $input
51+
* @param OutputInterface $output
52+
*
53+
* @return integer
54+
*/
55+
protected function execute(InputInterface $input, OutputInterface $output)
56+
{
57+
$command = implode(' ', $input->getArgument('input'));
58+
try {
59+
$execCommand = $this->commandBuilder->getExecCommand($command);
60+
} catch (\LogicException $e) {
61+
return $output->writeln($e->getMessage());
62+
}
63+
64+
$this->process->setCommandLine($execCommand);
65+
$this->process->setTty(true);
66+
try {
67+
$this->process->run(function ($type, $buffer) use ($output) {
68+
$output->write($buffer);
69+
});
70+
} catch (\RuntimeException $e) {
71+
$this->logger->error($e, ['command' => $command]);
72+
$output->writeln("Error occured, for more info check logs");
73+
} catch (\LogicException $e) {
74+
$this->logger->error($e, ['command' => $command]);
75+
$output->writeln($e->getMessage());
76+
}
77+
78+
return 0;
79+
}
80+
}

Command/Phinx/CommandBuilder.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PCF\MagentoMigration\Command\Phinx;
6+
7+
use mikehaertl\shellcommand\Command;
8+
use PCF\MagentoMigration\Api\PathLocatorInterface;
9+
use PCF\MagentoMigration\Api\ConfigBuilderInterface;
10+
11+
class CommandBuilder
12+
{
13+
14+
/** @var PathLocatorInterface */
15+
protected $pathLocator;
16+
17+
/** @var ConfigBuilderInterface */
18+
protected $configBuilder;
19+
20+
/** @var Command */
21+
protected $command;
22+
23+
/**
24+
* Phinx constructor.
25+
* @param PathLocatorInterface $pathLocator
26+
* @param ConfigBuilderInterface $configBuilder
27+
* @param Command $command
28+
*/
29+
public function __construct(
30+
PathLocatorInterface $pathLocator,
31+
ConfigBuilderInterface $configBuilder,
32+
Command $command
33+
) {
34+
$this->pathLocator = $pathLocator;
35+
$this->configBuilder = $configBuilder;
36+
$this->command = $command;
37+
}
38+
39+
40+
/**
41+
* @param string $method optional
42+
* @return string
43+
*/
44+
public function getExecCommand($method = '') :string
45+
{
46+
$this->command->useExec = true;
47+
$this->command->setCommand($this->pathLocator->getPhinxBinaryPath());
48+
$this->command->addArg('--configuration=', $this->configBuilder->createConfigPath());
49+
if (!empty($method)) {
50+
$this->command->addArg($method);
51+
}
52+
53+
return $this->command->getExecCommand();
54+
}
55+
}

Command/Phinx/ConfigBuilder.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PCF\MagentoMigration\Command\Phinx;
6+
7+
use Magento\Framework\App\DeploymentConfig;
8+
use Magento\Framework\Config\ConfigOptionsListConstants;
9+
use Magento\Framework\Exception\LocalizedException;
10+
use PCF\MagentoMigration\Api\ConfigBuilderInterface;
11+
use PCF\MagentoMigration\Api\PathLocatorInterface;
12+
use Symfony\Component\Yaml\Dumper;
13+
14+
class ConfigBuilder implements ConfigBuilderInterface
15+
{
16+
/** @var DeploymentConfig */
17+
protected $deploymentConfig;
18+
19+
/** @var PathLocatorInterface */
20+
protected $pathLocator;
21+
22+
/** @var Dumper */
23+
protected $yamlDumper;
24+
25+
protected $defaultDb = 'default';
26+
27+
/**
28+
* @param DeploymentConfig $deploymentConfig
29+
* @param PathLocatorInterface $pathLocator
30+
* @param Dumper $yamlDumper
31+
*/
32+
public function __construct(
33+
DeploymentConfig $deploymentConfig,
34+
PathLocatorInterface $pathLocator,
35+
Dumper $yamlDumper
36+
) {
37+
$this->deploymentConfig = $deploymentConfig;
38+
$this->pathLocator = $pathLocator;
39+
$this->yamlDumper = $yamlDumper;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function createConfigPath() :string
46+
{
47+
$yml = $this->yamlDumper->dump($this->getConfigArray(), 5);
48+
$tmpFileName = tempnam(sys_get_temp_dir(), md5($yml));
49+
file_put_contents($tmpFileName, $yml);
50+
51+
return $tmpFileName;
52+
}
53+
54+
/**
55+
* @param string $defaultDb
56+
*/
57+
protected function setDefaultDb(string $defaultDb)
58+
{
59+
$this->defaultDb = $defaultDb;
60+
}
61+
62+
/**
63+
* @return array
64+
*/
65+
protected function getConfigArray(): array
66+
{
67+
return [
68+
'paths' => [ 'migrations' => $this->pathLocator->getAllMigrationDirs()],
69+
'environments' => $this->getEnvironments()
70+
];
71+
}
72+
73+
/**
74+
* @throws LocalizedException
75+
* todo: make port and charset configurable
76+
*/
77+
protected function getDbConfig()
78+
{
79+
$magentoConfig = $this->deploymentConfig->getConfigData(ConfigOptionsListConstants::CONFIG_PATH_DB);
80+
81+
if (empty($magentoConfig['connection'])) {
82+
throw new LocalizedException(__('no db connection given'));
83+
}
84+
85+
$connections = [];
86+
foreach ($magentoConfig['connection'] as $envName => $config) {
87+
$connections[$envName] = [
88+
'adapter' => 'mysql',
89+
'host' => $config['host'],
90+
'name' => $config['dbname'],
91+
'user' => $config['username'],
92+
'pass' => $config['password'],
93+
'port' => 3306,
94+
'charset' => 'utf8'
95+
];
96+
}
97+
98+
if (empty($magentoConfig['connection']['default'])) {
99+
$this->setDefaultDb(key($magentoConfig['connection']));
100+
}
101+
102+
return $connections;
103+
}
104+
105+
/**
106+
* @return array
107+
*/
108+
protected function getEnvironments() : array
109+
{
110+
$return = [
111+
'default_migration_table' => 'phinxlog',
112+
'default_database' => $this->defaultDb,
113+
];
114+
115+
return array_merge($return, $this->getDbConfig());
116+
}
117+
}

0 commit comments

Comments
 (0)