Skip to content

Commit

Permalink
Use fake composer class when the autoload file does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Mar 5, 2020
1 parent 70c6fc0 commit af05ac5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Support/ComposerClassMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@

class ComposerClassMap
{
/** @var \Composer\Autoload\ClassLoader */
/** @var \Composer\Autoload\ClassLoader|FakeComposer */
protected $composer;

/** @var string */
protected $basePath;

public function __construct(?string $autoloaderPath = null)
{
$this->composer = require $autoloaderPath ?? base_path('/vendor/autoload.php');
$autoloaderPath = $autoloaderPath ?? base_path('/vendor/autoload.php');

if (file_exists($autoloaderPath)) {
$this->composer = require $autoloaderPath;
} else {
$this->composer = new FakeComposer();
}
$this->basePath = app_path();
}

Expand Down
21 changes: 21 additions & 0 deletions src/Support/FakeComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Facade\Ignition\Support;

class FakeComposer
{
public function getClassMap()
{
return [];
}

public function getPrefixes()
{
return [];
}

public function getPrefixesPsr4()
{
return [];
}
}
20 changes: 20 additions & 0 deletions tests/Support/ComposerClassMapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Facade\Ignition\Tests\Support;

use Facade\Ignition\Support\ComposerClassMap;
use Facade\Ignition\Tests\TestCase;

class ComposerClassMapTest extends TestCase
{
/** @test */
public function it_uses_fake_classmap_if_the_autoloader_does_not_exist()
{
$classMap = new ComposerClassMap('invalid');

$this->assertSame([], $classMap->listClasses());
$this->assertSame([], $classMap->listClassesInPsrMaps());
$this->assertNull($classMap->searchClassMap('SomeClass'));
$this->assertNull($classMap->searchPsrMaps('SomeClass'));
}
}

0 comments on commit af05ac5

Please sign in to comment.