What, why and how of PHP Composer
laravelfactory.com
laravelfactory.com
Recent example: Re-organizing namespaces and classes in an internal library. Losing my mind (doing everything right) until finally realizing Composer wasn't updating the autoloader to reflect changes to the lib's composer.json.
For something that works really well when it’s working, I have blown a lot of hours fighting with this stuff.
NPM? Wake up on a saturday morning and hot reload + react tools no longer works? F me.
On the bright side I almost finished the front end, so I can casually mess around with the solution every few days as I do my daily work in laravel.
Also, package.json is arguably a pretty gnarly way to organize... Well, everything? It's the kitchen sink of configuration, but it's supposed to be for managing packages. But no, you can also configure your packages in there, configure scripts for your application, etc. It's nuts.
Even so, both of them are miles ahead of what we had 10 years ago!
Yours is not really a critique of composer, since it's not a daemon or a continuously run script. You run it once and done.
watch -n 10 composer dumpautoloaf --no-scripts &
Full disclosure: I did not verify this command would work well`composer dump-autoload`
This will dump the autoloader and rebuild it. This is much much faster than an entire `composer install` run. For additional usage options try:
`composer help dump-autoload`
* Unrelated: HN can we please get better markdown?
If you're a PHP developer, here's why you should use a package manager instead of downloading zip-files.
This just reflects the warped incentives of a community more interested in hype, resume padding and currying influence in ecosystem with questionable package managers and frameworks than any technical scrutiny.
PHP was always simple to setup and use, the system package manager has all the extensions. App deployment was a breeze anyone could do it. On the other hand with Ruby or Node you need a full build environment, compilation and dependency hell just to deploy apps. Yet according according to some its PHP that is 'in the dark ages'. See the warped incentives and user hostile behavior.
It difficult to see how this makes PHP better and hopefully it's attempt to 'gain respect' will not take it down the same path.
In short, give composer a try if you are already building apps in PHP.
Sorry if I misunderstood what you're trying to say but this makes no sense to me. It's still the same simplicity to install PHP. But what you do with the package manager is not the same thing as what you achieve with composer. PHP extensions != PHP libraries.
You can also use the package manager to install python, ruby, perl, etc. What you generally don't do is use the package manager to install random 3rd party libraries (unless it's a really big project that's been packaged. e.g.: phpmyadmin)
Surely you wouldn't want php library maintainers to also create deb's and rpm's of their projects and host their own repositories, deal with distro release politics and other hindrances just so you can use apt or yum to install them?
Out of curiosity, what made you choose PHP as a primary language 4 years ago?
* Use full version numbers for your dependencies.
* Include the vendor folder in your VCS.
The last one I had to think about a lot but it's a real pain to get an old project working again when dependencies are no longer available. Unfortunately this happens a lot.You should only version composer.lock and composer.json files. Ignore vendor directory and leave that job to your build step in CI/CD...
Committing vendor directory may or may not be a good idea. But having your own copy of all dependencies (current and past) is necessary.
Definitely! Git mirrors are cheap, and there are various solutions for composer proxies out there. If you consider CI/CD you will want freedom from external calls anyway.
You should never VCS the vendor directory. If you are concerned about disappearing 3rd party dependencies, you should:
- have a local cache - keep e.g. git mirrors for 3rd party dependencies - invest a bit more time in selecting dependencies with higher quality.
I've seen repos with vendors checked in. A few years, hundreds of thousands of commits, and even a git status takes 30 seconds to run.
Just saying, there is a more sensible approach that beats storing vendors in the project :-)
That's a problem with your vcs, not with the (defensive) practice of tracking external dependencies.
Seriously, version control systems are specifically built to handle that kind of thing. You're mistaken as to the cause of your perceived slowdown.
Never say never, either. You don't know my life.
As for including the vendor dir in your VCS repo, this comes with quite a few drawbacks. That's one of the things Private Packagist (https://packagist.com) aims to fix though, as it keeps mirrored copies of your dependencies' zip archives so you get more reliable installs.
But it is possible to avoid packagist. Ugly and painful maybe but possible, and YMMV regarding the effort being worth the reward. Dependency management in PHP isn't (or shouldn't be) like it is with JS/Node where there is one authoritative registry that everyone has to depend on and that can assert direct influence over the community as a result. Packagist shouldn't be the PHP package registry, but a registry.
The tradeoff for this approach is to have to resolve version conflicts manually anytime there is more than minor dependency changes.
In particular, adding a package that is newer than most of your dependencies can be a nightmare when you locked down all the working revisions of your libraries.
On one hand there is less drama about packages changing under one's feet, on the other hand part of composer's job is shifted back to the dev who does the package updates.
Personally I would advise a more balanced approach of allowing minor version changes and only block major ones.
I get why they'd do this because obviously they want to capture potential buyers of their software to pester at a later date to buy but I'm not a potential buyer until I've tried it.
So I'm another one that won't be handing over any money.
The funny thing is apart from emailing the free code we almost never contact anyone since we know 99% of our free users are not potential paying customers and so there is no point wasting our time on them. We only know this because they give us their real email address which allows us to screen them out.
In any case, I finally decided to register using a throwaway spambox from Gmail instead of Mailinator (what difference does it make, really...) and it turns out the Laravel Factory is inferior to Voyager as it doesn't support roles out of the box. Which is a pity as other things have been solved quite nicely. I'll give it a try in a few months, maybe they catch up.
In our case we block all free and disposable email address since nobody without a corporate or university email address uses our service (it is aimed at scientists).
We used to let anyone use the service for free without registering for small scale use, but what we found were some commercial suppliers (our target market) were just telling their customers to use our free service rather than fork out the $0.02 it would have cost them to buy a license on assays they were selling for $10. Once cutoff from the ability to freeload, quite a number of these commercial suppliers decided to buy a license from us.
The good thing about this is not only did we get the business, but their customers got the benefit of our service automatically.
Initially, we used it just to make sure that system is not misused but we understand your concern and accept it.
We are also considering the roles part for authorization. Stay tuned. Thanks.
I've been on the fence for a while, but I'm still not sure yet if this is right for me.
Most of the projects I work on have minimal dependncies & existing autoloading strategies. While I would like to have access to new dependencies via Composer it seems like I'll be forced to engage in heavy refactoring to update namespaces and will need to somehow package my existing code.
Curious if anyone here has firsthand experience adding Composer to an existing project that wasn't dependency heavy?
Everything you already have will continue to work fine. All you have to do is include the autoloader somewhere in your code path.
An example assuming you already installed composer on the machine into your /bin or similar directory.
# add a new dependency:
composer require guzzlehttp/guzzle
# this dependency will now be in the vendor directory ls ./vendor
# update your apps entry point (ie: frontcontroller.php): // require composer's autoloader
require ('vendor/autoload.php');
# autoload will now find any dependencies you add via composer // create guzzle client
$client = new \GuzzleHttp\Client();
# another tip, if you already have a bunch of class files some where (ie: myapp/src/classes , myapp/src/models ) You can include class paths via composer.json "autoload": {
"classmap": ["classes/", "models/", "MyClass.php"]
}
You can now remove any hard-coded include or require statements.There is a lot more things you can do with it but this is the most basic example.
One useful feature is custom commands[0].
[0]https://getcomposer.org/doc/articles/scripts.md#writing-cust...
Also, regarding autoloading, I find that once you get used to the structure of PSR-4, adding files to a PSR-4 namespace[1] and just having autoloading work is incredibly helpful. The only pain point is dealing with namespaces everywhere.
‍ What, Why and How of PHP Composer
The zwj doesn't print (of course) but the space does, causing the alignment of titles on the front page to be slightly off. It tweaked me.Always looking out for the important things...
Coupled with arcane version syntax (Drupal's version system predates Composer and it used natural syntax like foo (>=2.1.1) bar (2.x) so all they needed to do is to take the about 30 lines of well commented code, no need to memorize a list of sigils but no), excruciating slowness makes me wish I never heard of it much less need to work with it daily.
As for trust, noone in the PHP world! Absolutely noone! I got into a debate with Rasmus about the meaning of backwards compatibility around the time PHP 5.4 began to throw additional warnings (not a new class of errors which could be additionally displayed, no) on code that previously worked. Drupal 8.4 released with a known bug that caused some code to merrily lose files uploaded by users because of a BC break. If you want BC, look at the kernel Linux API and even the ABI.
`composer require {vendor}/{your-amazing-package}:{version}`