I don't understand the description of the composer dump-autoload command :
If you need to update the autoloader because of new classes in a classmap package for example, you can use "dump-autoload" to do that without having to go through an install or update.
Additionally, it can dump an optimized autoloader that converts PSR-0/4 packages into classmap ones for performance reasons. In large applications with many classes, the autoloader can take up a substantial portion of every request's time. Using classmaps for everything is less convenient in development, but using this option you can still use PSR-0/4 for convenience and classmaps for performance.
Why does it says dump ... for performance reasons ? Why dumping the optimized loader ?
Is it 'dumping for' or 'converts for performance reason' ?
If it's 'dumping for performance', why then it says classmaps for performance ?
If it's 'converts for performance', why then dumping the autoloader ?
I'm confused.
It dumps a classmap that is used by the autoloader. This way, the autoloader doesn't need to search through the filesystem to find the correct file, it already knows the file. This will save time.
Classmaps are not faster in every case. Be careful and measure the benefit you'd get from them before using it in production.
See my answer here for details: Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster?
Related
Some PHP-libraries can be used after installing them via composer.
What this mean?
Is that only way to use those libraries or is there a way to use them copying the code in to correct location and referring them in the code?
Examples:
mPDF can be used (only?) via composer
https://mpdf.github.io/
PHPMailer can be used just copying files in correct location and referring to them
https://github.com/PHPMailer/PHPMailer
The thing with libraries is, that they might require other libraries and those libraries might require other libraries and so on. So downloading them and putting them in some location will be tedious and when two libraries require the same library in a different version you will run into problems. Composer will solve this issue for you by figuring out which libraries are needed to resolve all requirements and make sure to download a version that fits all or raise an error that the current collection of libraries contains incompatible libraries and which ones.
The other problem is finding the right location to store those libraries since PHP has to figure out where each class is stored you will either have to add require/include statements to your code and libraries which is tedious and will complicate future updates, e.g. when classes are renamed or removed. A way around this is is having a shared lib directory, but then you will run into problems when you have multiple projects requiring different library versions.
For libraries composer is the de facto standard and you will always need/want to install libraries in your project with it. It takes care of resolving correct versions, autoloading and updating making it incredibly helpful, especially if you were still around when composer was not a thing. I would use it even if I need no libraries just for the autoloading and the ability to later add libraries, when my project grows/changes.
edit: Even PHPMailer provides a composer.json even though it does not require other libraries, but if you install it via composer you can make sure that your system fulfills the requirements (PHP version & installed extensions) which you might miss otherwise, leading to a possibly long debugging session figuring out why some feature won't work.
edit You can use composer for projects on shared hosting as well. Instead of running the command on the server, you must then run it on your local machine or build server for the actual server. You can copy your project including the vendor folder to your shared host and things should work. The vendor folder contains all libraries and the autoload.php and can be copied along with your code.
In order to do that reliably, in your composer.json under config you can specify the platform you will run your code on. You should define the correct PHP version at the very least, but the installed extensions as well, to make sure you don't accidentally install libraries where you don't have the required extension. When you run composer install or composer update it will use these platform details as a basis to download libraries that match them. This is especially important when you have PHP 7 installed, but your host does not have it yet.
When running composer on a separate server than the one that uses code, a few options will not work like --apcu-autoloader, but you probably don't use them anyway.
If you run composer on your local machine and copy stuff over, you can improve the experience a bit by adding a few options to your composer install:
composer install --no-dev --prefer-dist --classmap-authoritative
You can get details about these options in the documentation. The important things are:
--no-dev, to possibly reduce the number of libraries downloaded to the ones needed for production (because we only want to run the project on our server not develop on it).
--prefer-dist (why is explained in the docs)
--classmap-authoritative or --optimize-autoloader, (the first might not work with some projects/libraries) but it will improve autoloading making your application a teeny, tiny bit faster in production
The first 2 options, you should not run if you copy your development environment, as they will not provide all dependencies for development. Maybe setup a second project that is just used for checking out the latest changes from git, running tests to make sure everything works, then removing vendor & running that command, (possibly make some changes to the config for prod) and finally copy things to your shared hosting environment. If you use something like gitlab that provides CI-capabilities, you could also do these steps on the ci-server and let that copy stuff, but it takes some time to set things up.
So i recently came to the decision that I should start using composer to better manage my projects.
This stemmed from the fact that not to long ago I started using requireJS for my javascript and found it very useful.
The problem I am having is I currently do not use any PHP libraries and feel composer solves a problem I do not have.
So my Question is:
Should I focus on learning and using composer even before I know of any useful libraries? Or would I be better served exploring libraries until I reach a need for composer to manage them?
Should composer be used in a similar manner to requireJS to help me modulate my php code?
I'd like to just pull the following package in using composer so it will be autoloaded, but can't figure out how (or if) that can be done:
https://code.google.com/p/freshbooks-php-library/
Is this possible, and if so, how?
I know the question is a little old, but I don't think you can include packages, archives or libraries without the target resource containing a (valid) composer.json in the project root.
However; Google has given me this repo https://github.com/Isinlor/freshbooks-php which would seem to do the trick.
I have a project where I can't install or use composer.
However, I want to use the OmniPay library, which is a composer package. So I want to just take the libraries out and use them as standard libraries in Code Igniter.
This is what I get from the Composer install (below). If I take them into my application/libraries/ folder, how can i load them into my controller?
I think the easiest way would be to actually USE Composer on the machine you are developing on, and then commit the vendor folder to the repository.
If you don't want to do this, you'd have to manually do the steps Composer does. And this is more complicated than you think:
The last steps would be to extract the source from the package, put it somewhere suitable, and recreate the autoloading for it. This is the easy step, because the autoloading usually is PSR-0 or PSR-4. "Onmipay/Common" has one addtional class that doesn't fit into this and is loaded as classmap by Composer, so this class has to be handled extra.
The more complicated step before this: Omnipay is also using "guzzle/http" as a HTTP client, and "symfony/http-foundation". These packages also need to be included both as a file and then with their respective autoloading. And these also have dependencies with autoloading and dependencies...
Let Composer do the error-prone work, and commit the vendor folder if you can't provide the infrastructure to add the dependencies later during deployment.
With my code I'm using autoload for lazy evaluation so I can load the program faster and load the files when needed, I'm not seeing so many people using it, but with Thin project I noticed autoload has been used widely, anyways just wanna know if there is any risk to use it .
autoload is not threadsafe and will be deprecated in the future versions of Ruby. Here is the proof by Matz (creator of ruby).