Skip to content

Commit

Permalink
Merge pull request #249 from /issues/232-missing-php-classes
Browse files Browse the repository at this point in the history
Improve new dependencies autoloading
  • Loading branch information
T2L committed Oct 15, 2021
2 parents b9f7082 + 0684830 commit 28bde6e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Draft Environment (Unreleased)

- [GH-232](https://github.com/lemberg/draft-environment/pull/232) - Improve new dependencies autoloading
- [GH-220](https://github.com/lemberg/draft-environment/pull/220) - Store last applied update in the vm-settings.yml file instead of composer.lock
- [GH-245](https://github.com/lemberg/draft-environment/pull/245) - Fix MySQL fail to install due to incorrect configuration of the SQL mode ('~' is not a valid configuration)

Expand Down
39 changes: 31 additions & 8 deletions src/Config/Manager/AbstractConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace Lemberg\Draft\Environment\Config\Manager;

use Nette\IOException;
use Nette\Utils\Finder;
use Composer\Autoload\ClassLoader;
use Composer\Autoload\ClassMapGenerator;
use Composer\Composer;
use Composer\IO\IOInterface;
use Consolidation\Comments\Comments;
use Lemberg\Draft\Environment\App;
use Lemberg\Draft\Environment\Config\AbstractStepInterface;
use Lemberg\Draft\Environment\Config\Config;
Expand Down Expand Up @@ -50,9 +53,7 @@ final public function __construct(Composer $composer, IOInterface $io, Config $c

// This code is running in Composer context, newly added packages might
// not be autoloaded.
if (!class_exists(RobotLoader::class)) {
$this->autoloadDependencies();
}
$this->autoloadDependencies();
}

/**
Expand Down Expand Up @@ -140,17 +141,39 @@ final protected function getLastAvailableUpdateWeight(): int {
}

/**
* Manually autoload dependencies from Nette framework.
* Manually autoload new dependencies.
*
* @link https://github.com/lemberg/draft-environment/issues/232
*/
private function autoloadDependencies(): void {
$loader = new ClassLoader();

$vendorDir = $this->composer->getConfig()->get('vendor-dir');
foreach (['nette/utils', 'nette/finder', 'nette/robot-loader'] as $path) {
$loader->addClassMap(ClassMapGenerator::createMap("$vendorDir/$path"));
$classes = [
'classmap' => [
RobotLoader::class => 'nette/robot-loader',
Finder::class => 'nette/finder',
IOException::class => 'nette/utils',
],
'psr4' => [
Comments::class => [
'package' => 't2l/comments',
'prefix' => 'Consolidation\\Comments\\',
'path' => 'src',
],
],
];

foreach ($classes['classmap'] as $class_name => $package_name) {
if (!class_exists($class_name)) {
$loader->addClassMap(ClassMapGenerator::createMap("$vendorDir/$package_name"));
}
}

$loader->register();
foreach ($classes['psr4'] as $class_name => $autoload_data) {
if (!class_exists($class_name)) {
$loader->addPsr4($autoload_data['prefix'], $vendorDir . '/' . $autoload_data['package'] . '/' . $autoload_data['path']);
}
}
}

}

0 comments on commit 28bde6e

Please sign in to comment.