Skip to content

Commit

Permalink
MAGECLOUD-4100: Introduce DI component (magento#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
oshmyheliuk authored and shiftedreality committed Oct 8, 2019
1 parent 3347db4 commit 5d62e21
Show file tree
Hide file tree
Showing 24 changed files with 252 additions and 400 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
"graylog2/gelf-php": "^1.4.2",
"guzzlehttp/guzzle": "^6.2",
"illuminate/config": "^5.6",
"illuminate/container": "^5.6",
"illuminate/contracts": "^5.6",
"magento/magento-cloud-components": "^1.0.1",
"monolog/monolog": "^1.16",
"psr/container": "^1.0",
"psr/log": "^1.0",
"symfony/console": "^2.3||^4.0",
"symfony/config": "^3.4||^4.3",
"symfony/dependency-injection": "^3.4||^4.3",
"symfony/process": "^2.1||^4.1",
"symfony/serializer": "^4.2",
"symfony/yaml": "^4.0"
Expand Down
61 changes: 61 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults autowire="true" autoconfigure="true" public="true"/>
<!-- ... -->
<prototype namespace="Magento\MagentoCloud\" resource="../src/*" exclude="../src/{Test}"/>

<service id="Composer\Package\Version\VersionParser" autowire="false" />
<service id="Magento\MagentoCloud\Filesystem\SystemList" autowire="false" />
<service id="Magento\MagentoCloud\Step\Build\DeployStaticContent" autowire="false" />
<service id="Magento\MagentoCloud\App\Container" autowire="false" />
<service id="Magento\MagentoCloud\App\GenericException" autowire="false" />
<service id="Magento\MagentoCloud\App\Logger\Gelf\Handler" autowire="false" />
<service id="Magento\MagentoCloud\App\Container" autowire="false" />
<service id="Magento\MagentoCloud\Config\Validator\Result\Error" autowire="false" />
<service id="Magento\MagentoCloud\DB\Data\Connection" autowire="false" />
<service id="Magento\MagentoCloud\DB\Data\RelationshipConnection" autowire="false" />
<service id="Magento\MagentoCloud\DB\PDOException" autowire="false" />
<service id="Magento\MagentoCloud\Docker\ConfigurationMismatchException" autowire="false" />
<service id="Magento\MagentoCloud\Filesystem\Flag\ConfigurationMismatchException" autowire="false" />
<service id="Magento\MagentoCloud\Scenario\Exception\ProcessorException" autowire="false" />
<service id="Magento\MagentoCloud\Scenario\Exception\ValidationException" autowire="false" />
<service id="Magento\MagentoCloud\Filesystem\FileSystemException" autowire="false" />
<service id="Magento\MagentoCloud\Package\UndefinedPackageException" autowire="false" />
<service id="Magento\MagentoCloud\Step\Deploy\DeployCompletion" autowire="false" />
<service id="Magento\MagentoCloud\Step\Deploy\DeployStaticContent" autowire="false" />
<service id="Magento\MagentoCloud\Step\Deploy\InstallUpdate" autowire="false" />
<service id="Magento\MagentoCloud\Step\Deploy\InstallUpdate\ConfigUpdate" autowire="false" />
<service id="Magento\MagentoCloud\Step\Deploy\InstallUpdate\Install" autowire="false" />
<service id="Magento\MagentoCloud\Step\Deploy\InstallUpdate\Update" autowire="false" />
<service id="Magento\MagentoCloud\Step\Deploy\PreDeploy" autowire="false" />
<service id="Magento\MagentoCloud\Step\StepComposite" autowire="false" />
<service id="Magento\MagentoCloud\Step\StepException" autowire="false" />
<service id="Magento\MagentoCloud\Step\ValidateConfiguration" autowire="false" />
<service id="Magento\MagentoCloud\Step\Build\BackupData" autowire="false" />
<service id="Magento\MagentoCloud\Service\ServiceMismatchException" autowire="false" />
<service id="Magento\MagentoCloud\Shell\Process" autowire="false" />
<service id="Magento\MagentoCloud\Shell\ProcessException" autowire="false" />
<service id="Magento\MagentoCloud\Filesystem\Flag\Pool">
<argument type="collection">
<argument key="regenerate">var/.regenerate</argument>
<argument key="scd_in_build">.static_content_deploy</argument>
<argument key="deploy_is_failed">var/.deploy_is_failed</argument>
</argument>
</service>
<service id="Composer\Package\Version\VersionParser"/>
<service id="Composer\Semver\VersionParser"/>
<service id="Composer\Semver\Semver"/>
<service id="Composer\Semver\Comparator"/>
<service id="Illuminate\Config\Repository"/>
<service id="Symfony\Component\Serializer\Encoder\XmlEncoder"/>
<service id="Psr\Log\LoggerInterface" alias="Magento\MagentoCloud\App\Logger" />
<service id="Magento\MagentoCloud\App\ContainerInterface" alias="container" />
<service id="Magento\MagentoCloud\Shell\ShellInterface" alias="Magento\MagentoCloud\Shell\Shell" />
<service id="Magento\MagentoCloud\Config\Stage\DeployInterface" alias="Magento\MagentoCloud\Config\Stage\Deploy" />
<service id="Magento\MagentoCloud\Config\Stage\BuildInterface" alias="Magento\MagentoCloud\Config\Stage\Build" />
</services>
</container>
4 changes: 4 additions & 0 deletions scenario/build/generate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<step name="set-production-mode" type="Magento\MagentoCloud\Step\SetProductionMode"/>
<step name="validate-configuration" type="Magento\MagentoCloud\Step\ValidateConfiguration">
<arguments>
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
<argument name="validators" xsi:type="array">
<item name="500" xsi:type="array">
<item name="composer" xsi:type="object">Magento\MagentoCloud\Config\Validator\Build\ComposerFile</item>
Expand All @@ -35,6 +36,9 @@
<step name="dump-autoload" type="Magento\MagentoCloud\Step\Build\ComposerDumpAutoload"/>
<step name="deploy-static-content" type="Magento\MagentoCloud\Step\Build\DeployStaticContent">
<arguments>
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
<argument name="flag-manager" xsi:type="object">Magento\MagentoCloud\Filesystem\Flag\Manager</argument>
<argument name="scd-on-build" xsi:type="object">Magento\MagentoCloud\Config\Validator\GlobalStage\ScdOnBuild</argument>
<argument name="steps" xsi:type="array">
<item name="generate" xsi:type="object">Magento\MagentoCloud\Step\Build\DeployStaticContent\Generate</item>
</argument>
Expand Down
1 change: 1 addition & 0 deletions scenario/build/transfer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<step name="clear-init-directory" type="Magento\MagentoCloud\Step\Build\ClearInitDirectory"/>
<step name="backup-data" type="Magento\MagentoCloud\Step\Build\BackupData">
<arguments>
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
<argument name="steps" xsi:type="array">
<item name="static-content" xsi:type="object">Magento\MagentoCloud\Step\Build\BackupData\StaticContent</item>
<item name="writable-dirs" xsi:type="object">Magento\MagentoCloud\Step\Build\BackupData\WritableDirectories</item>
Expand Down
11 changes: 11 additions & 0 deletions scenario/deploy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<step name="remove-deploy-failed-flag" type="Magento\MagentoCloud\Step\Deploy\RemoveDeployFailedFlag"/>
<step name="pre-deploy" type="Magento\MagentoCloud\Step\Deploy\PreDeploy">
<arguments>
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
<argument name="steps" xsi:type="array">
<item name="cache" xsi:type="object">Magento\MagentoCloud\Step\Deploy\PreDeploy\ConfigUpdate\Cache</item>
<item name="clean-static-content" xsi:type="object">Magento\MagentoCloud\Step\Deploy\PreDeploy\CleanStaticContent</item>
Expand All @@ -24,6 +25,7 @@
<step name="disable-cron" type="Magento\MagentoCloud\Step\Deploy\DisableCron"/>
<step name="validate-config" type="Magento\MagentoCloud\Step\ValidateConfiguration">
<arguments>
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
<argument name="validators" xsi:type="array">
<item name="500" xsi:type="array">
<item name="database-configuration" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\DatabaseConfiguration</item>
Expand Down Expand Up @@ -53,6 +55,8 @@
<step name="set-crypt-key" type="Magento\MagentoCloud\Step\Deploy\SetCryptKey"/>
<step name="install-update" type="Magento\MagentoCloud\Step\Deploy\InstallUpdate">
<arguments>
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
<argument name="state" xsi:type="object">Magento\MagentoCloud\Config\State</argument>
<argument name="installSteps" xsi:type="array">
<item name="setup" xsi:type="object">Magento\MagentoCloud\Step\Deploy\InstallUpdate\Install\Setup</item>
<!-- Config update start -->
Expand Down Expand Up @@ -88,6 +92,11 @@
</step>
<step name="deploy-static-content" type="Magento\MagentoCloud\Step\Deploy\DeployStaticContent">
<arguments>
<argument name="flag-manager" xsi:type="object">Magento\MagentoCloud\Filesystem\Flag\Manager</argument>
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
<argument name="deploy" xsi:type="object">Magento\MagentoCloud\Config\Stage\DeployInterface</argument>
<argument name="global-section" xsi:type="object">Magento\MagentoCloud\Config\GlobalSection</argument>
<argument name="static-content-cleaner" xsi:type="object">Magento\MagentoCloud\Util\StaticContentCleaner</argument>
<argument name="steps" xsi:type="array">
<item name="generate" xsi:type="object">Magento\MagentoCloud\Step\Deploy\DeployStaticContent\Generate</item>
</argument>
Expand All @@ -98,6 +107,8 @@
<!-- This process runs processes if only post_deploy hook is not configured -->
<step name="deploy-complete" type="Magento\MagentoCloud\Step\Deploy\DeployCompletion">
<arguments>
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
<argument name="hook-checker" xsi:type="object">Magento\MagentoCloud\Config\Application\HookChecker</argument>
<argument name="steps" xsi:type="array">
<item name="enable-cron" xsi:type="object">Magento\MagentoCloud\Step\PostDeploy\EnableCron</item>
<item name="backup" xsi:type="object">Magento\MagentoCloud\Step\PostDeploy\Backup</item>
Expand Down
1 change: 1 addition & 0 deletions scenario/post-deploy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<step name="is-deploy-failed" type="Magento\MagentoCloud\Step\PostDeploy\DeployFailed"/>
<step name="validate-config" type="Magento\MagentoCloud\Step\ValidateConfiguration">
<arguments>
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
<argument name="validators" xsi:type="array">
<item name="300" xsi:type="array">
<item name="debug-logging" xsi:type="object">Magento\MagentoCloud\Config\Validator\Deploy\DebugLogging</item>
Expand Down
152 changes: 51 additions & 101 deletions src/App/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

namespace Magento\MagentoCloud\App;

use Composer\Composer;
use Composer\Factory;
use Composer\IO\BufferIO;
use Magento\MagentoCloud\Filesystem\SystemList;
use Magento\MagentoCloud\Step\Deploy as DeployStep;
use Magento\MagentoCloud\Step\StepComposite;
use Magento\MagentoCloud\Step\StepInterface;
use Composer;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

/**
* @inheritdoc
Expand All @@ -21,7 +24,7 @@
class Container implements ContainerInterface
{
/**
* @var \Illuminate\Container\Container
* @var \Symfony\Component\DependencyInjection\Container
*/
private $container;

Expand All @@ -30,105 +33,49 @@ class Container implements ContainerInterface
* @param string $magentoBasePath
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @throws \Exception
*/
public function __construct(string $toolsBasePath, string $magentoBasePath)
{
/**
* Creating concrete container.
*/
$this->container = new \Illuminate\Container\Container();
$containerBuilder = new ContainerBuilder();
$containerBuilder->set('container', $containerBuilder);
$containerBuilder->setDefinition('container', new Definition(Container::class))
->setArguments([$toolsBasePath, $magentoBasePath]);

$systemList = new SystemList($toolsBasePath, $magentoBasePath);

/**
* Instance configuration.
*/
$this->container->instance(ContainerInterface::class, $this);
$this->container->instance(SystemList::class, $systemList);

/**
* Binding.
*/
$this->container->singleton(Composer\Composer::class, static function () use ($systemList) {
$composerFactory = new Composer\Factory();
$composerFile = file_exists($systemList->getMagentoRoot() . '/composer.json')
? $systemList->getMagentoRoot() . '/composer.json'
: $systemList->getRoot() . '/composer.json';

return $composerFactory->createComposer(
new Composer\IO\BufferIO(),
$composerFile,
false,
$systemList->getMagentoRoot()
);
});

/**
* Singletons.
*/
$this->container->singleton(\Magento\MagentoCloud\Config\Environment::class);
$this->container->singleton(\Magento\MagentoCloud\Config\State::class);
$this->container->singleton(\Magento\MagentoCloud\App\Logger\Pool::class);
$this->container->singleton(\Magento\MagentoCloud\Package\Manager::class);
$this->container->singleton(\Magento\MagentoCloud\Package\MagentoVersion::class);
$this->container->singleton(\Magento\MagentoCloud\Config\Stage\Build::class);
$this->container->singleton(\Magento\MagentoCloud\Config\Stage\Deploy::class);
$this->container->singleton(\Magento\MagentoCloud\Config\Stage\PostDeploy::class);

/**
* Interface to implementation binding.
*/
$this->container->singleton(
\Magento\MagentoCloud\Config\ConfigInterface::class,
\Magento\MagentoCloud\Config\Shared::class
);
$this->container->singleton(
\Magento\MagentoCloud\Shell\ShellInterface::class,
\Magento\MagentoCloud\Shell\Shell::class
);
$this->container->singleton(
\Magento\MagentoCloud\DB\DumpInterface::class,
\Magento\MagentoCloud\DB\Dump::class
);
$this->container->singleton(
\Psr\Log\LoggerInterface::class,
\Magento\MagentoCloud\App\Logger::class
);
$this->container->singleton(
\Magento\MagentoCloud\DB\ConnectionInterface::class,
\Magento\MagentoCloud\DB\Connection::class
);
$this->container->singleton(
\Magento\MagentoCloud\Config\Stage\BuildInterface::class,
\Magento\MagentoCloud\Config\Stage\Build::class
);
$this->container->singleton(
\Magento\MagentoCloud\Config\Stage\DeployInterface::class,
\Magento\MagentoCloud\Config\Stage\Deploy::class
);
$this->container->singleton(
\Magento\MagentoCloud\Config\Stage\PostDeployInterface::class,
\Magento\MagentoCloud\Config\Stage\PostDeploy::class
);
$this->container->singleton(
\Magento\MagentoCloud\PlatformVariable\DecoderInterface::class,
\Magento\MagentoCloud\PlatformVariable\Decoder::class
);
$this->container->singleton(
\Magento\MagentoCloud\Config\Database\ConfigInterface::class,
\Magento\MagentoCloud\Config\Database\MergedConfig::class
$containerBuilder->set(SystemList::class, $systemList);
$containerBuilder->setDefinition(SystemList::class, new Definition(SystemList::class));

$containerBuilder->set(Composer::class, $this->createComposerInstance($systemList));
$containerBuilder->setDefinition(Composer::class, new Definition(Composer::class));

$loader = new XmlFileLoader($containerBuilder, new FileLocator($toolsBasePath . '/config/'));
$loader->load('services.xml');
$containerBuilder->compile();

$this->container = $containerBuilder;
}

/**
* @param SystemList $systemList
* @return Composer
*/
private function createComposerInstance(SystemList $systemList): Composer
{
$composerFactory = new Factory();
$composerFile = file_exists($systemList->getMagentoRoot() . '/composer.json')
? $systemList->getMagentoRoot() . '/composer.json'
: $systemList->getRoot() . '/composer.json';

$composer = $composerFactory->createComposer(
new BufferIO(),
$composerFile,
false,
$systemList->getMagentoRoot()
);

$this->container->when(DeployStep\InstallUpdate\ConfigUpdate\Urls::class)
->needs(StepInterface::class)
->give(function () {
return $this->container->makeWith(StepComposite::class, [
'steps' => [
$this->container->make(DeployStep\InstallUpdate\ConfigUpdate\Urls\Database::class),
$this->container->make(DeployStep\InstallUpdate\ConfigUpdate\Urls\Environment::class),
],
]);
});
return $composer;
}

/**
Expand All @@ -138,7 +85,7 @@ public function __construct(string $toolsBasePath, string $magentoBasePath)
*/
public function get($id)
{
return $this->container->make($id);
return $this->container->get($id);
}

/**
Expand All @@ -152,17 +99,20 @@ public function has($id): bool
/**
* @inheritdoc
*/
public function set(string $abstract, $concrete, bool $shared = true): void
public function set(string $id, $service): void
{
$this->container->forgetInstance($abstract);
$this->container->bind($abstract, $concrete, $shared);
$this->container->set($id, $service);
}

/**
* @inheritdoc
*/
public function create(string $abstract, array $params = [])
{
return $this->container->make($abstract, $params);
if (empty($params) && $this->has($abstract)) {
return $this->get($abstract);
}

return new $abstract(...array_values($params));
}
}
7 changes: 3 additions & 4 deletions src/App/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ public function create(string $abstract, array $params = []);
/**
* Register a binding with the container.
*
* @param string $abstract
* @param string|\Closure $concrete
* @param bool $shared
* @param string $id
* @param object $service
* @return void
*/
public function set(string $abstract, $concrete, bool $shared = true): void;
public function set(string $id, $service): void;
}
Loading

0 comments on commit 5d62e21

Please sign in to comment.