问题
Our legacy PHP code includes tcpdf (https://github.com/tecnickcom/TCPDF) as part of the code base.
I am trying to move it out to a vendor
folder, so I added Composer to the project, added TCPDF to composer.json
and updated.
But the config/tcpdf_config.php
file is modified in our code base (custom PDF author name etc.), and rightfully so, according to the docs: http://www.tcpdf.org/installation.php
Now, I'm not sure it's a good idea to modify vendor/tecnick.com/tcpdf/config/tcpdf_config.php
because it might be overwritten by Composer any time I update. Also, there is not a word about Composer in the tcpdf docs.
What is the right solution to configure tcpdf (or any third-party library used through Composer, for that matter) while allowing Composer updates?
回答1:
The way you are supposed to inject your configuration is to define all the constants first before ever touching the first TCPDF class.
Make sure to also set the constant K_TCPDF_EXTERNAL_CONFIG
to true. This will prevent the autoconfiguration to search for the file you were talking about. (See line 60 of this file here: http://sourceforge.net/p/tcpdf/code/ci/master/tree/tcpdf_autoconfig.php)
This is well hidden in the documentation, but I found this: http://www.tcpdf.org/doc/code/example__019_8php.html
How to override TCPDF config using Composer
- Copy the original
tcpdf_config.php
somewhere to your project, for examplesrc/tcpdf_config.php
. - Add
define('K_TCPDF_EXTERNAL_CONFIG', true);
at the beginning of your config copy and modify the rest of the config to your needs. - Edit your
composer.json
and add/updateautoload
section:
...
"autoload": {
...
"files": [
"src/tcpdf_config.php",
...
]
}
...
- Regenerate the composer autoloader using
composer dump-autoload
.
来源:https://stackoverflow.com/questions/28700659/how-to-configure-tcpdf-when-installing-with-composer