Skip to content
This repository has been archived by the owner on Mar 27, 2019. It is now read-only.

Commit

Permalink
Fixed dependency issues.
Browse files Browse the repository at this point in the history
Signed-off-by: crynobone <crynobone@gmail.com>
  • Loading branch information
crynobone committed Dec 22, 2013
1 parent afcd76d commit 94a6442
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 26 deletions.
103 changes: 89 additions & 14 deletions src/Orchestra/Optimize/Compiler.php
Expand Up @@ -34,6 +34,40 @@ class Compiler
*/
protected $components = array();

/**
* Arrange priority for compilation.
*
* @var array
*/
protected $arrange = array(
'translation',
'testbench',
'support',
'debug',
'model',
'memory',
'facile',
'asset',
'auth',
'extension',
'html',
'resources',
'view',
'warden',
'widget',
'foundation',
);

/**
* Files collection.
*
* @var array
*/
protected $collection = array(
'added' => array(),
'missing' => array(),
);

/**
* Construct a new instance.
*
Expand All @@ -57,26 +91,67 @@ public function __construct(Repository $config, Filesystem $files, $path, array
*/
public function run()
{
$failed = array();
$compile = $this->config->get('compile', array());
// Run compilation based on class dependencies.
$this->runCompilationByArrangedPriority();

// Run remaining components (if any).
foreach ($this->components as $name => $classes) {
foreach ($classes as $class) {
$file = "{$this->path}/orchestra/{$name}/src/{$class}.php";

if ($this->files->exists($file)) {
$compile[] = $file;
} else {
$failed[] = $file;
}
}
$this->compileGroupClasses($name, $classes);
}

// Append application file to the bottom.
$original = $this->config->get('compile', array());

foreach ($original as $class) {
$this->collection['added'][] = $class;
}

$this->config->set('compile', $compile);
$this->config->set('compile', $this->collection['added']);

return new Fluent(array(
'succeed' => $compile,
'failed' => $failed,
'added' => $this->collection['added'],
'missing' => $this->collection['missing'],
));
}

/**
* Compilation should be appended by priority to avoid class redeclared
* issue.
*
* @return void
*/
protected function runCompilationByArrangedPriority()
{
foreach ($this->arrange as $name) {
if (! isset($this->components[$name])) {
continue;
}

$classes = $this->components[$name];

$this->compileGroupClasses($name, $classes);

unset($this->components[$name]);
}
}

/**
* Compile classes by group.
*
* @param string $name
* @param array $classes
* @return void
*/
protected function compileGroupClasses($name, array $classes)
{
foreach ($classes as $class) {
$file = "{$this->path}/orchestra/{$name}/src/{$class}.php";

if ($this->files->exists($file)) {
$this->collection['added'][] = $file;
} else {
$this->collection['missing'][] = $file;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Orchestra/Optimize/OptimizeCommand.php
Expand Up @@ -56,9 +56,9 @@ public function fire()
*/
protected function addOptimizableClasses()
{
$compiled = $this->compiler->run();
$collection = $this->compiler->run();

foreach ($compiled->failed as $file) {
foreach ($collection->missing as $file) {
$this->comment("File not found: [{$file}]");
}
}
Expand Down
35 changes: 25 additions & 10 deletions tests/CompilerTest.php
Expand Up @@ -22,31 +22,46 @@ public function tearDown()
*/
public function testRunMethod()
{
$config = m::mock('\Illuminate\Config\Repository');
$files = m::mock('\Illuminate\Filesystem\Filesystem');
$path = '/var/www/laravel/vendor';
$config = m::mock('\Illuminate\Config\Repository');
$files = m::mock('\Illuminate\Filesystem\Filesystem');
$path = '/var/www/laravel/vendor';

$components = array(
'asset' => array(
'AssetServiceProvider',
'NoneExistClass',
),
'foo' => array(
'FooServiceProvider',
),
);

$succeed = array("{$path}/orchestra/asset/src/AssetServiceProvider.php");
$failed = array("{$path}/orchestra/asset/src/NoneExistClass.php");
$added = array(
"{$path}/orchestra/asset/src/AssetServiceProvider.php",
"{$path}/orchestra/foo/src/FooServiceProvider.php",
"app/Foobar.php",
);
$missing = array(
"{$path}/orchestra/asset/src/NoneExistClass.php",
);

$config->shouldReceive('get')->once()->with('compile', array())->andReturn(array())
->shouldReceive('set')->once()->with('compile', $succeed)->andReturn(null);
$config->shouldReceive('get')->once()->with('compile', array())
->andReturn(array(
"app/Foobar.php",
))
->shouldReceive('set')->once()->with('compile', $added)->andReturn(null);
$files->shouldReceive('exists')->once()
->with("{$path}/orchestra/asset/src/AssetServiceProvider.php")->andReturn(true)
->shouldReceive('exists')->once()
->with("{$path}/orchestra/asset/src/NoneExistClass.php")->andReturn(false);
->with("{$path}/orchestra/asset/src/NoneExistClass.php")->andReturn(false)
->shouldReceive('exists')->once()
->with("{$path}/orchestra/foo/src/FooServiceProvider.php")->andReturn(true);
$stub = new Compiler($config, $files, $path, $components);
$compiled = $stub->run();

$expected = new Fluent(array(
'succeed' => $succeed,
'failed' => $failed,
'added' => $added,
'missing' => $missing,
));

$this->assertEquals($expected, $compiled);
Expand Down

0 comments on commit 94a6442

Please sign in to comment.