Skip to content

Commit 8725599

Browse files
committed
Make installation more robust
* Remove autoload.php generation * Make using the puli/cli in the same project work
1 parent 8b71670 commit 8725599

File tree

2 files changed

+75
-77
lines changed

2 files changed

+75
-77
lines changed

src/PuliPluginImpl.php

+50-39
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
use Composer\Composer;
1515
use Composer\Config;
1616
use Composer\IO\IOInterface;
17+
use Composer\Json\JsonFile;
1718
use Composer\Package\AliasPackage;
1819
use Composer\Package\PackageInterface;
1920
use Composer\Script\Event;
21+
use Composer\Util\RemoteFilesystem;
2022
use Exception;
2123
use RuntimeException;
2224
use Symfony\Component\Filesystem\Filesystem;
@@ -103,11 +105,6 @@ class PuliPluginImpl
103105
*/
104106
private $initialized = false;
105107

106-
/**
107-
* @var string
108-
*/
109-
private $autoloadFile;
110-
111108
public function __construct(Event $event, PuliRunner $puliRunner = null)
112109
{
113110
$this->composer = $event->getComposer();
@@ -116,38 +113,19 @@ public function __construct(Event $event, PuliRunner $puliRunner = null)
116113
$this->isDev = $event->isDevMode();
117114
$this->puliRunner = $puliRunner;
118115
$this->rootDir = Path::normalize(getcwd());
119-
120-
$vendorDir = $this->config->get('vendor-dir');
121-
122-
// On TravisCI, $vendorDir is a relative path. Probably an old Composer
123-
// build or something. Usually, $vendorDir should be absolute already.
124-
$vendorDir = Path::makeAbsolute($vendorDir, $this->rootDir);
125-
126-
$this->autoloadFile = $vendorDir.'/autoload.php';
127116
}
128117

129118
public function preAutoloadDump()
130119
{
131-
if (!$this->initialized) {
132-
$this->initialize();
133-
}
134-
135-
// This method is called twice. Run it only once.
120+
// This method is called multiple times. Run it only once.
136121
if (!$this->runPreAutoloadDump) {
137122
return;
138123
}
139124

140125
$this->runPreAutoloadDump = false;
141126

142-
try {
143-
$factoryClass = $this->getConfigKey('factory.in.class');
144-
$factoryFile = $this->getConfigKey('factory.in.file');
145-
} catch (PuliRunnerException $e) {
146-
$this->printWarning('Could not load Puli configuration', $e);
147-
148-
return;
149-
}
150-
127+
$factoryClass = $this->getConfigKeyFromJsonFile('factory.in.class');
128+
$factoryFile = $this->getConfigKeyFromJsonFile('factory.in.file');
151129
$factoryFile = Path::makeAbsolute($factoryFile, $this->rootDir);
152130

153131
$autoload = $this->composer->getPackage()->getAutoload();
@@ -178,7 +156,7 @@ public function postAutoloadDump()
178156
$this->initialize();
179157
}
180158

181-
// This method is called twice. Run it only once.
159+
// This method is called multiple times. Run it only once.
182160
if (!$this->runPostAutoloadDump) {
183161
return;
184162
}
@@ -193,8 +171,15 @@ public function postAutoloadDump()
193171
return;
194172
}
195173

196-
$this->insertFactoryClassConstant($this->autoloadFile, $factoryClass);
197-
$this->setBootstrapFile($this->autoloadFile);
174+
$vendorDir = $this->config->get('vendor-dir');
175+
176+
// On TravisCI, $vendorDir is a relative path. Probably an old Composer
177+
// build or something. Usually, $vendorDir should be absolute already.
178+
$vendorDir = Path::makeAbsolute($vendorDir, $this->rootDir);
179+
180+
$autoloadFile = $vendorDir.'/autoload.php';
181+
$this->insertFactoryClassConstant($autoloadFile, $factoryClass);
182+
$this->setBootstrapFile($autoloadFile);
198183
}
199184

200185
/**
@@ -206,7 +191,7 @@ public function postInstall()
206191
$this->initialize();
207192
}
208193

209-
// This method is called twice. Run it only once.
194+
// This method is called multiple times. Run it only once.
210195
if (!$this->runPostInstall) {
211196
return;
212197
}
@@ -251,13 +236,6 @@ public function postInstall()
251236

252237
private function initialize()
253238
{
254-
if (!file_exists($this->autoloadFile)) {
255-
$filesystem = new Filesystem();
256-
// Avoid problems if using the runner before autoload.php has been
257-
// generated
258-
$filesystem->dumpFile($this->autoloadFile, '');
259-
}
260-
261239
$this->initialized = true;
262240

263241
// Keep the manually set runner
@@ -518,7 +496,7 @@ private function setBootstrapFile($autoloadFile)
518496
/**
519497
* Loads Composer's currently installed packages.
520498
*
521-
* @return PackageInterface[] The installed packages indexed by their names.
499+
* @return PackageInterface[] The installed packages indexed by their names
522500
*/
523501
private function loadComposerPackages()
524502
{
@@ -533,6 +511,39 @@ private function loadComposerPackages()
533511
return $packages;
534512
}
535513

514+
private function getConfigKeyFromJsonFile($key)
515+
{
516+
$value = null;
517+
$jsonPath = realpath(substr($this->config->get('vendor-dir'), 0, -strlen($this->config->get('vendor-dir', Config::RELATIVE_PATHS)))).DIRECTORY_SEPARATOR.'puli.json';
518+
if (file_exists($jsonPath)) {
519+
$jsonFile = new JsonFile($jsonPath, new RemoteFilesystem($this->io));
520+
$config = $jsonFile->read();
521+
if (empty($config['version']) || '1.0' !== $config['version']) {
522+
throw new \RuntimeException('Invalid configuration version schema of puli.json file!');
523+
}
524+
}
525+
switch ($key) {
526+
case 'factory.in.file':
527+
if (empty($config) || empty($config['config']['factory']['in']['file'])) {
528+
$value = '.puli/GeneratedPuliFactory.php';
529+
} else {
530+
$value = $config['config']['factory']['in']['file'];
531+
}
532+
break;
533+
case 'factory.in.class':
534+
if (empty($config) || empty($config['config']['factory']['in']['class'])) {
535+
$value = 'Puli\\GeneratedPuliFactory';
536+
} else {
537+
$value = $config['config']['factory']['in']['class'];
538+
}
539+
break;
540+
default:
541+
throw new \RuntimeException(sprintf('Cannot extract key "%s" from config!', $key));
542+
}
543+
544+
return $value;
545+
}
546+
536547
private function getConfigKey($key)
537548
{
538549
$value = trim($this->puliRunner->run('config %key% --parsed', array(

tests/PuliPluginImplTest.php

+25-38
Original file line numberDiff line numberDiff line change
@@ -1717,36 +1717,27 @@ public function testDoNotRemovePuliDirIfParentOfRootDirectory()
17171717

17181718
public function testInsertFactoryClassIntoClassMap()
17191719
{
1720-
$this->puliRunner->expects($this->at(0))
1721-
->method('run')
1722-
->with('-V')
1723-
->willReturn('Puli version '.PuliPluginImpl::MIN_CLI_VERSION."\n");
1724-
17251720
$this->rootPackage->setAutoload(array('classmap' => array('src')));
17261721

17271722
$this->plugin->preAutoloadDump();
17281723

1729-
$this->assertSame(array('classmap' => array('src', $this->tempDir)), $this->rootPackage->getAutoload());
1724+
$this->assertSame(array('classmap' => array('src', $this->tempDir.'/.puli/GeneratedPuliFactory.php')), $this->rootPackage->getAutoload());
17301725
}
17311726

17321727
public function testCreatesStubFactoryClassWithNamespaceIfDoesNotExist()
17331728
{
1734-
$this->puliRunner->expects($this->at(0))
1735-
->method('run')
1736-
->with('-V')
1737-
->willReturn('Puli version '.PuliPluginImpl::MIN_CLI_VERSION."\n");
1738-
$this->puliRunner->expects($this->at(1))
1739-
->method('run')
1740-
->with('config %key% --parsed', array(
1741-
'key' => 'factory.in.class',
1742-
))
1743-
->willReturn("Puli\\MyFactory\n");
1744-
$this->puliRunner->expects($this->at(2))
1745-
->method('run')
1746-
->with('config %key% --parsed', array(
1747-
'key' => 'factory.in.file',
1748-
))
1749-
->willReturn(".puli/factory.class\n");
1729+
$jsonConfig = array(
1730+
'version' => '1.0',
1731+
'config' => array(
1732+
'factory' => array(
1733+
'in' => array(
1734+
'class' => 'Puli\\MyFactory',
1735+
'file' => '.puli/factory.class'
1736+
)
1737+
)
1738+
)
1739+
);
1740+
file_put_contents($this->tempDir.'/puli.json', json_encode($jsonConfig));
17501741

17511742
$this->plugin->preAutoloadDump();
17521743

@@ -1755,22 +1746,18 @@ public function testCreatesStubFactoryClassWithNamespaceIfDoesNotExist()
17551746

17561747
public function testCreatesStubFactoryClassWithoutNamespaceIfDoesNotExist()
17571748
{
1758-
$this->puliRunner->expects($this->at(0))
1759-
->method('run')
1760-
->with('-V')
1761-
->willReturn('Puli version '.PuliPluginImpl::MIN_CLI_VERSION."\n");
1762-
$this->puliRunner->expects($this->at(1))
1763-
->method('run')
1764-
->with('config %key% --parsed', array(
1765-
'key' => 'factory.in.class',
1766-
))
1767-
->willReturn("MyFactory\n");
1768-
$this->puliRunner->expects($this->at(2))
1769-
->method('run')
1770-
->with('config %key% --parsed', array(
1771-
'key' => 'factory.in.file',
1772-
))
1773-
->willReturn(".puli/factory.class\n");
1749+
$jsonConfig = array(
1750+
'version' => '1.0',
1751+
'config' => array(
1752+
'factory' => array(
1753+
'in' => array(
1754+
'class' => 'MyFactory',
1755+
'file' => '.puli/factory.class'
1756+
)
1757+
)
1758+
)
1759+
);
1760+
file_put_contents($this->tempDir.'/puli.json', json_encode($jsonConfig));
17741761

17751762
$this->plugin->preAutoloadDump();
17761763

0 commit comments

Comments
 (0)