Skip to content

Commit 367e79f

Browse files
author
Ivan Gavryshko
committed
Merge remote-tracking branch 'ogreLib/MAGETWO-34697-component-management-package-update'
2 parents f5f1ce8 + bceb9e2 commit 367e79f

12 files changed

+807
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/vendor
2+
/.cache
3+
/.metadata
4+
/.project
5+
/.settings
6+
atlassian*
7+
/.idea
8+
/.gitattributes

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Magento composer library helps to instantiate Composer application and run composer commands.

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "magento/composer",
3+
"description": "Magento composer library helps to instantiate Composer application and run composer commands.",
4+
"type": "library",
5+
"version": "1.0.0",
6+
"license": [
7+
"OSL-3.0",
8+
"AFL-3.0"
9+
],
10+
"require": {
11+
"php": "~5.5.0|~5.6.0",
12+
"composer/composer": "1.0.0-alpha10",
13+
"symfony/console": "~2.3 <2.7"
14+
},
15+
"require-dev": {
16+
"phpunit/phpunit": "4.1.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Magento\\Composer\\": "src"
21+
}
22+
}
23+
}

phpunit.xml.dist

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
>
9+
<php>
10+
<ini name="error_reporting" value="-1" />
11+
</php>
12+
13+
<testsuites>
14+
<testsuite name="Magento Composer Library Test">
15+
<directory>./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory>./</directory>
22+
<exclude>
23+
<directory>./tests</directory>
24+
</exclude>
25+
</whitelist>
26+
</filter>
27+
</phpunit>

src/ConsoleArrayInputFactory.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Composer;
8+
9+
use Symfony\Component\Console\Input\ArrayInput;
10+
11+
/**
12+
* Symfony console ArrayInput factory
13+
*/
14+
class ConsoleArrayInputFactory
15+
{
16+
/**
17+
* Create arrayInput instance.
18+
*
19+
* @param array $params
20+
* @return ArrayInput
21+
*/
22+
public function create(array $params)
23+
{
24+
return new ArrayInput($params);
25+
}
26+
}

src/InfoCommand.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Composer;
8+
9+
/**
10+
* Class InfoCommand calls composer info command
11+
*/
12+
class InfoCommand
13+
{
14+
/**
15+
* Current version
16+
*/
17+
const CURRENT_VERSION = 'current_version';
18+
19+
const VERSIONS = 'versions';
20+
21+
/**
22+
* Available versions
23+
*/
24+
const AVAILABLE_VERSIONS = 'available_versions';
25+
26+
27+
/**
28+
* @var MagentoComposerApplication
29+
*/
30+
protected $magentoComposerApplication;
31+
32+
/**
33+
* Constructor
34+
*
35+
* @param MagentoComposerApplication $magentoComposerApplication
36+
*/
37+
public function __construct(MagentoComposerApplication $magentoComposerApplication)
38+
{
39+
$this->magentoComposerApplication = $magentoComposerApplication;
40+
}
41+
42+
/**
43+
* Runs composer info command
44+
*
45+
* @param string $package
46+
* @param bool $installed
47+
* @return array|bool
48+
*/
49+
public function run($package, $installed = false)
50+
{
51+
$commandParameters = [
52+
'command' => 'info',
53+
'package' => $package,
54+
'-i' => $installed
55+
];
56+
57+
$result = [];
58+
59+
try {
60+
$output = $this->magentoComposerApplication->runComposerCommand($commandParameters);
61+
} catch (\RuntimeException $e) {
62+
return false;
63+
}
64+
65+
$rawLines = explode(PHP_EOL, $output);
66+
67+
foreach ($rawLines as $line) {
68+
$chunk = explode(':', $line);
69+
if (count($chunk) === 2) {
70+
$result[trim($chunk[0])] = trim($chunk[1]);
71+
}
72+
}
73+
74+
$result = $this->extractVersions($result);
75+
76+
return $result;
77+
}
78+
79+
/**
80+
* Extracts package versions info
81+
*
82+
* @param array $packageInfo
83+
* @return array
84+
*/
85+
private function extractVersions($packageInfo)
86+
{
87+
$versions = explode(', ', $packageInfo[self::VERSIONS]);
88+
89+
if (count($versions) === 1) {
90+
$packageInfo[self::CURRENT_VERSION] = str_replace('* ', '', $packageInfo[self::VERSIONS]);
91+
$packageInfo[self::AVAILABLE_VERSIONS] = [];
92+
} else {
93+
$currentVersion = array_values(preg_grep("/^\*.*/", $versions));
94+
if ($currentVersion) {
95+
$packageInfo[self::CURRENT_VERSION] = str_replace('* ', '', $currentVersion[0]);
96+
} else {
97+
$packageInfo[self::CURRENT_VERSION] = '';
98+
}
99+
100+
$packageInfo[self::AVAILABLE_VERSIONS] = array_values(preg_grep("/^\*.*/", $versions, PREG_GREP_INVERT));
101+
}
102+
103+
return $packageInfo;
104+
}
105+
}

src/MagentoComposerApplication.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Composer;
8+
9+
use Composer\Console\Application;
10+
use Composer\IO\BufferIO;
11+
use Composer\Factory as ComposerFactory;
12+
use Symfony\Component\Console\Output\BufferedOutput;
13+
14+
/**
15+
* Class MagentoComposerApplication
16+
*
17+
* This class provides ability to set composer application settings and run any composer command.
18+
* Also provides method to get Composer instance so you can have access composer properties lie Locker
19+
*/
20+
class MagentoComposerApplication
21+
{
22+
23+
const COMPOSER_WORKING_DIR = '--working-dir';
24+
25+
/**
26+
* Path to Composer home directory
27+
*
28+
* @var string
29+
*/
30+
private $composerHome;
31+
32+
/**
33+
* Path to composer.json file
34+
*
35+
* @var string
36+
*/
37+
private $composerJson;
38+
39+
/**
40+
* Buffered output
41+
*
42+
* @var BufferedOutput
43+
*/
44+
private $consoleOutput;
45+
46+
/**
47+
* @var ConsoleArrayInputFactory
48+
*/
49+
private $consoleArrayInputFactory;
50+
51+
/**
52+
* @var Application
53+
*/
54+
private $consoleApplication;
55+
56+
/**
57+
* Constructs class
58+
*
59+
* @param string $pathToComposerHome
60+
* @param string $pathToComposerJson
61+
* @param Application $consoleApplication
62+
* @param ConsoleArrayInputFactory $consoleArrayInputFactory
63+
* @param BufferedOutput $consoleOutput
64+
*/
65+
public function __construct(
66+
$pathToComposerHome,
67+
$pathToComposerJson,
68+
Application $consoleApplication = null,
69+
ConsoleArrayInputFactory $consoleArrayInputFactory = null,
70+
BufferedOutput $consoleOutput = null
71+
) {
72+
$this->consoleApplication = $consoleApplication ? $consoleApplication : new Application();
73+
$this->consoleArrayInputFactory = $consoleArrayInputFactory ? $consoleArrayInputFactory
74+
: new ConsoleArrayInputFactory();
75+
$this->consoleOutput = $consoleOutput ? $consoleOutput : new BufferedOutput();
76+
77+
$this->composerJson = $pathToComposerJson;
78+
$this->composerHome = $pathToComposerHome;
79+
80+
putenv('COMPOSER_HOME=' . $pathToComposerHome);
81+
82+
$this->consoleApplication->setAutoExit(false);
83+
}
84+
85+
/**
86+
* Creates composer object
87+
*
88+
* @return \Composer\Composer
89+
* @throws \Exception
90+
*/
91+
public function createComposer()
92+
{
93+
return ComposerFactory::create(new BufferIO(), $this->composerJson);
94+
}
95+
96+
/**
97+
* Runs composer command
98+
*
99+
* @param array $commandParams
100+
* @param string|null $workingDir
101+
* @return bool
102+
* @throws \RuntimeException
103+
*/
104+
public function runComposerCommand(array $commandParams, $workingDir = null)
105+
{
106+
$this->consoleApplication->resetComposer();
107+
108+
if ($workingDir) {
109+
$commandParams[self::COMPOSER_WORKING_DIR] = $workingDir;
110+
} else {
111+
$commandParams[self::COMPOSER_WORKING_DIR] = dirname($this->composerJson);
112+
}
113+
114+
$input = $this->consoleArrayInputFactory->create($commandParams);
115+
116+
$exitCode = $this->consoleApplication->run($input, $this->consoleOutput);
117+
118+
if ($exitCode) {
119+
throw new \RuntimeException(
120+
sprintf('Command "%s" failed: %s', $commandParams['command'], $this->consoleOutput->fetch())
121+
);
122+
}
123+
124+
return $this->consoleOutput->fetch();
125+
}
126+
}

0 commit comments

Comments
 (0)