问题
So I'm creating a semi-popular open source project and am looking for ways for its users to customize their copy.
Basically I've zero experience using Composer and next to none with git submodules. I have this file structure pushed to git:
/ROOT
----/subdirectory/
---------/another.file.php
----/main.class.php
----/config.default.php
It would be ideal for users to be able to copy config.default.php
to the same directory, rename it to config.php
(and by doing that override the default configuration values) - and I'd like them to be able to add their own files to the /subdirectory/
too, allowing them to extend the tool to their unique requirements.
My question is, don't those files get trimmed when pull (in the case of git submodule) or Composer update is performed? If so, how do I achieve the requirements with as little fuss for the end user as possible:
- A single, optional, freely editable configuration file
- Two directories that can contain files that users create
- Above alterations stay within the users own version control system and are not removed when a new version is fetched.
Thank you for your patience in advance.
回答1:
What most application frameworks/CMSs are doing these days is to split this process in two packages:
- one library package (e.g. symfony/symfony, laravel/framework) that contains the bulk of the code, and that will be installed in vendor/
- one "bootstrap" package (e.g. symfony/framework-standard-edition, laravel/laravel) that contains an application shell that your users can start from. That would contain default config and a composer.json that includes the library package + whatever else is needed in the "require" section. This bootstrap package is typically not updated via composer since it becomes the application/site of your users. Therefore it should contain as little code as possible, and be essentially only configuration.
回答2:
You currently load your user config when your class loads:
if ( is_readable( KINT_DIR . 'config.php' ) ) {
require KINT_DIR . 'config.php';
}
If you added a public setConfig() property, then the config could be located anywhere, and your default would still work (code off the top of my wee head):
Public static function setConfig($config) {
if ( is_readable( $config ) ) {
require $config;
}
}
来源:https://stackoverflow.com/questions/18751125/allow-users-of-an-opensourced-project-to-add-their-own-files-to-their-copy