Magento2 skeleton
May 24, 2018
In my last post I described a setup which enables you to have your Magento2 modules outside of the magento-root.
This post will take things further so if you might want to read the Post Magento2 modules outside of magento-root first.
If you already know about path-repositories with relative paths and how to include local Magento modules, feel free to continue right away.
Prerequisites
As a reminder, here is the setup from the last post:
my-project
├── magento
| └── composer.json
└── src
└── namespace
└── my-module
└── composer.json
I would prefer to have the Magento composer.json in the document-root of the repository, so that we get this layout:
my-project
├── src
| └── namespace
| └── my-module
| └── composer.json
├── www
└── composer.json
Two things have changed with this layout.
First the composer.json
now is in the repository-root and secondly the magento-root is named www
.
At this point I want to thank Juan Alsonso for his post Proper Magento 2 Composer Setup. If you are interested in his setup, you should definetly head over and read it. Without his post the setup described in this post would not have been possible.
I will be using some of his ideas in this post.
Update to composer.json
As Juan pointed out, there need to be changes to some settings in the composer.json.
These are the path updates for www
directory that need to be done, since the magento-root is www
:
"config": {
"vendor-dir": "www/vendor",
"use-include-path": true
},
"extra": {
"magento-root-dir": "www/",
"magento-force": "override"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"Magento\\Setup\\": "www/setup/src/Magento/Setup/"
},
"files": [
"www/app/etc/NonComposerComponentRegistration.php"
]
},
"autoload-dev": {
"psr-4": {
"Magento\\Sniffs\\": "www/dev/tests/static/framework/Magento/Sniffs/",
"Magento\\Tools\\": "www/dev/tools/Magento/Tools/",
"Magento\\Tools\\Sanity\\": "www/dev/build/publication/sanity/Magento/Tools/Sanity/",
"Magento\\TestFramework\\Inspection\\": "www/dev/tests/static/framework/Magento/TestFramework/Inspection/",
"Magento\\TestFramework\\Utility\\": "www/dev/tests/static/framework/Magento/TestFramework/Utility/"
}
}
Scripts section in composer.json
The following scripts block is basically copied from Juan’s setup, just with updated directory and minus the symlinker tool.
"scripts": {
"preventRunningComposerInMagentoFolder": "[ ! $(basename `pwd`) = 'www' ] || { echo 'Checking current dir is not magento folder'; exit 1; }",
"setPermissionsBin": "chmod -R +x www/bin www/vendor/bin",
"setSymlinks": "cd www && ln -sf ../composer.* .",
"pre-install-cmd": [
"@preventRunningComposerInMagentoFolder"
],
"pre-update-cmd": [
"@preventRunningComposerInMagentoFolder"
],
"post-install-cmd": [
"@setPermissionsBin",
"@setSymlinks"
],
"post-update-cmd": [
"@setPermissionsBin",
"@setSymlinks"
]
}
}
Updating the path repository
The repository of type path needs to be updated, since the relative path to the src
directory has changed.
"repositories": [
{ "type": "path", "url": "src/*/*" },
{ "type": "composer", "url": "https://repo.magento.com/" }
]
Unchanged parts composer.json
The require block stays the same as before:
"require": {
"magento/product-community-edition": "2.2.4",
"composer/composer": "@alpha",
"my-namespace/my-local-module": "*",
"mwltr/ext-magento2-mwltr-filesystem-filedriver": "^1.0.1"
},
Running Composer Commands
With the composer.json being in the document-root, we can run composer update
instead of composer -d=<subd-dir> update
.
Making this setup more standard compliant, with having the composer.json in the repository-root.
Github repository
I have put toghether a github repository containing all the files mentioned above.
Additionally you can find a .gitignore
working with this setup, which basically ignores the whole www
directory.
Furthermore a phpunit.xml
is provided to run unit tests with this setup.
The repository can be found here: mwr/demo-magento2-skeleton
Summary
This is the basic setup I am currently using as a template for my projects (plus some more files and directories for tools I use).
If you have any suggestions or optimizations feel free to comment or leave a pull-request or issue on github. Enjoy!