[Question] Moving composer.json
OwenMelbz opened this issue · 2 comments
Hi - love the idea of this package, however having some problems customising something.
We want to use our root composer.json
to handle the dependencies rather than one inside a theme folder.
When I add it to our main one via composer require soberwp/controller
it installs fine.
We then update the autoload paths in the root composer to
"autoload": {
"psr-4": {
"App\\": "web/app/themes/theme-name/app/"
}
}
Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Class 'App' not found in ............
Any ideas how to get around this?
Thanks
In all my WordPress projects, I use a Bedrock-style structure and ignore any Composer manifests in themes.
Lately with Sage 9, I had an issue that looks like yours: Sober autoloads the controller.php
file. This file expects to be included during the loading of the theme. Unfortunately, by loading at an earlier point it can't internally resolve the target location.
My solution was to create a Composer plugin—mcaskill/composer-exclude-files—that allows one to ignore a dependency's files
from inclusion in Composer's autoload_files.php
. This way, one can (if they need it) explicitly require those files
later on where needed.
Example:
Example 1.1: Root composer.json
"require": {
"soberwp/controller": "^2.1"
},
"extra": {
"exclude-from-files": [
"soberwp/controller/controller.php"
]
}
Example 1.2: Theme web/app/themes/foobar/functions.php
require $vendorDir . '/soberwp/controller/controller.php'
Example 2: Sage 9 Theme web/app/themes/sage/resources/functions.php
array_map(function ($file) use ($sage_error) {
if (!file_exists($file = BASEPATH . '/vendor'. '/' . $file . '.php')) {
$sage_error(
sprintf(
__('Error locating <code>%s</code> for inclusion.', 'sage'),
$file
),
'File not found'
);
}
require $file;
}, [ 'soberwp/controller/controller' ]);
Related
@mcaskill nice solution