Skip to content

Commit

Permalink
Merge pull request #41 from BrianHenryIE/autoload-override
Browse files Browse the repository at this point in the history
Allow overriding packages' autoload key
  • Loading branch information
coenjacobs authored May 23, 2020
2 parents d63631b + 7ec7e92 commit 799d3ac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ Mozart requires little configuration. All you need to do is tell it where the bu
"packages": [
"pimple/pimple"
],
"override_autoload": {
"google/apiclient": {
"classmap": [
"src/"
]
}
},
"delete_vendor_directories": true
}
},
Expand All @@ -45,6 +52,7 @@ The following configuration is optional:

- `delete_vendor_directories` is a boolean flag to indicate if the packages' vendor directories should be deleted after being processed. _default: true_.
- `packages` is an optional array that defines the packages to be processed by Mozart. The array requires the slugs of packages in the same format as provided in your `composer.json`. Mozart will automatically rewrite dependencies of these packages as well. You don't need to add dependencies of these packages to the list. If this field is absent, all packages listed under composer require will be included.
- `override_autoload` a dictionary, keyed with the package names, of autoload settings to replace those in the original packages' `composer.json` `autoload` property.

After Composer has loaded the packages as defined in your `composer.json` file, you can now run `mozart compose` and Mozart will bundle your packages according to the above configuration. It is recommended to dump the autoloader after Mozart has finished running, in case there are new classes or namespaces generated that aren't included in the autoloader yet.

Expand Down
12 changes: 8 additions & 4 deletions src/Composer/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ class Package
/** @var array */
public $dependencies = [];

public function __construct($path)
{
$this->path = $path;
$this->config = json_decode(file_get_contents($this->path . '/composer.json'));
public function __construct($path, $overrideAutoload = null)
{
$this->path = $path;
$this->config = json_decode(file_get_contents($this->path . '/composer.json'));

if (isset($overrideAutoload)) {
$this->config->autoload = $overrideAutoload;
}
}

public function findAutoloaders()
Expand Down
7 changes: 6 additions & 1 deletion src/Console/Commands/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ private function findPackages($slugs)
continue;
}

$package = new Package($packageDir);
$autoloaders = null;
if (isset($this->config->override_autoload) && isset($this->config->override_autoload->$package_slug) ){
$autoloaders = $this->config->override_autoload->$package_slug;
}

$package = new Package($packageDir, $autoloaders);
$package->findAutoloaders();

$config = json_decode(file_get_contents($packageDir . 'composer.json'));
Expand Down

0 comments on commit 799d3ac

Please sign in to comment.