How to configure tcpdf when installing with Composer?

非 Y 不嫁゛ 提交于 2021-01-27 12:31:03

问题


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

  1. Copy the original tcpdf_config.php somewhere to your project, for example src/tcpdf_config.php.
  2. Add define('K_TCPDF_EXTERNAL_CONFIG', true); at the beginning of your config copy and modify the rest of the config to your needs.
  3. Edit your composer.json and add/update autoload section:
...
"autoload": {
  ...
  "files": [
    "src/tcpdf_config.php",
    ...
  ]
}
...
  1. Regenerate the composer autoloader using composer dump-autoload.


来源:https://stackoverflow.com/questions/28700659/how-to-configure-tcpdf-when-installing-with-composer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!