Articles

New Composer Patterns

by Global Employees Software Development Company
Here’s a brief update on some features which became available within the past year in Composer, which will come in handy when you hire PHP Developer in India.

Checking dependencies for poor patterns

You might know of the composer validate command; however, did you have knowledge of its new --with-dependencies / -A flag? It’ll let you validate both the project and all of your dependencies at one time!

It’s nice to check if any of your dependencies possesses unsafe requirements such as >= or likewise problems. Also, it’s possible to combine it with --strict to ensure that all warnings result in a failure exit code, in order for you to detect warnings in the CI builds for example by validating the command exit code.

Try it: composer validate -A –strict

Defining the target production environment within composer.json

The option config.platform allows you to emulate what platform packages are available on the prod environment. This way even if you have the latest PHP version or are missing a certain extension locally for instance, composer always will solve packages assuming you have the packages that you declared installed.

Let us take one example. If I’m running PHP 5.6 within production, yet utilize PHP 7 to develop upon my machine, I may wind up installing a package which depends upon PHP 7 and not see the issue until I deploy and things break upon the server. Obviously it’s best to develop with the same versions in order to avoid any surprises, yet this is not always practical and particularly while working on open-source libraries I believe most do not use VMs yet instead work with whichever PHP they have upon the host system.

Excluding paths from optimized classmap

As you run composer dump-autoload -o in order to get an optimized autoloader, it’ll scan all of your files and build a massiveclassmap, even for packages which define autoload regulations as psr-0 or psr-4. It’s good; however, in some instance you have a few classes within the psr-4 path which you actually do not want to be included within the optimized map. A typical instance of this might be Symfony2 bundles which follow all best practices layout of having every source at the root of the repo. Within this example the psr-4 path is "" (repo root); there’s a Tests/ folder containing the test classes. In production we obviously do not want to involve these test classes within the optimized class map as it’s a waste. Adding a 2nd line to the autoloadconfig is going to ensure they aren’t involved:

"autoload": {
    "psr-4": { "Nelmio\\CorsBundle\\": "" },
    "exclude-from-classmap": ["/Tests/"]
},

Require packages safely and easily

For some time now we’ve had the capability of operating composer require some/package without having to specify the version. Composer discovers the best requirement for you. But it came with a catch, as it always chose the most recent available version. Usually, this works yet if the most recent version calls for a newer PHP version than what you might have on your machine it actually would fail. When you hire PHP Developer in India, they may fix that and it’ll look at your PHP version to decide what the best version to install is.

Sponsor Ads


About Global Employees Advanced   Software Development Company

131 connections, 0 recommendations, 442 honor points.
Joined APSense since, June 23rd, 2016, From West Sacramento, United States.

Created on Dec 31st 1969 18:00. Viewed 0 times.

Comments

No comment, be the first to comment.
Please sign in before you comment.