Maintaining Package Versions3:40 with Alena Holligan
Composer plays an important role in package versioning. This versioning is built around a standard versioning system called Semantic Versioning, allowing you to stay current on security updates and upgrade as new features become available.
Testing Version Constraints
There is a handy packagist semver checker (short for symantic versioning) that allows you to test out your version constraints.
Version Usage Documentation
Exact - You can specify the exact version of a package. This will tell Composer to install this version and this version only. If other dependencies require a different version, the solver will ultimately fail and abort any install or update procedures.
Range - By using comparison operators you can specify ranges of valid versions. Valid operators are >, >=, <, <=, !=.
You can define multiple ranges. Ranges separated by a space ( ) or comma (,) will be treated as a logical AND. A double pipe (||) will be treated as a logical OR. AND has higher precedence than OR.
Note: Be careful when using unbounded ranges as you might end up unexpectedly installing versions that break backwards compatibility. Consider using the caret operator instead for safety.
>=1.0 >=1.0 <2.0 >=1.0 <1.1 || >=1.2*
Range (Hyphen) - Inclusive set of versions. Partial versions on the right include are completed with a wildcard. For example 1.0 - 2.0 is equivalent to >=1.0.0 <2.1 as the 2.0 becomes 2.0.*. On the other hand 1.0.0 - 2.1.0 is equivalent to >=1.0.0 <=2.1.0.
1.0 - 2.0
Wildcard - You can specify a pattern with a * wildcard. 1.0.* is the equivalent of >=1.0 <1.1.
Next Significant Release Operators
Tilde - The ~ operator is best explained by example: ~1.2 is equivalent to >=1.2 <2.0.0, while ~1.2.3 is equivalent to >=1.2.3 <1.3.0. As you can see it is mostly useful for projects respecting semantic versioning. A common usage would be to mark the minimum minor version you depend on, like ~1.2 (which allows anything up to, but not including, 2.0). Since in theory there should be no backwards compatibility breaks until 2.0, that works well. Another way of looking at it is that using ~ specifies a minimum version, but allows the last digit specified to go up.
Caret - The ^ operator behaves very similarly but it sticks closer to semantic versioning, and will always allow non-breaking updates. For example ^1.2.3 is equivalent to >=1.2.3 <2.0.0 as none of the releases until 2.0 should break backwards compatibility. For pre-1.0 versions it also acts with safety in mind and treats ^0.3 as >=0.3.0 <0.4.0.
This is the recommended operator for maximum interoperability when writing library code.
Composer plays another important role by helping us maintain package versioning. 0:00 This feature has a couple important benefits. 0:05 First, it allows us to stay current on any security updates 0:08 to external packages that we're using in our application. 0:12 Second, it allows us to easily upgrade as new features become available. 0:16 So how does versioning work and how do we use composer to maintain versioning? 0:20 Composer version control is built around a standard versioning system 0:27 called semantic versioning. 0:31 Under this system, version numbers and the way they change convey meaning about 0:33 the underlying code and what has been modified from one version to the next. 0:38 The first position signifies major changes 0:43 that break the way things work in the previous version. 0:46 The second position signifies minor changes such as new features that 0:50 add functionality without changing current functionality. 0:55 And finally, the third position signifies patches such as bug fixes or 0:59 security updates. 1:04 Incrementing on any of these numbers signifies a change. 1:05 Let's take P.H.P. for example in September of 1:09 2016 P.H.P. released version 7.0.11. 1:13 7 stands for the major version, 1:18 0 is the minor version, and 11 is the current patch. 1:21 As a general rule, upgrading to a minor release or patch within the same 1:26 major release should be backwards compatible and not break any of your code. 1:31 This standard is widely accepted, 1:37 however, not every package you use is guaranteed to follow these guidelines. 1:39 So it's a good idea to check with the package you wish to use. 1:44 Let's now go see package versioning in action with composer. 1:49 Let's start by taking a look at what we already have. 1:54 We use the command line to install packages up to this point. 1:57 Composer uses a composer.json file for configuration and 2:01 you can modify this file manually. 2:05 When we open the file, you can see the four packages we installed, but 2:10 not their dependencies. 2:14 Composer handles the package dependencies for us. 2:16 You can add, remove and change packages and versions directly in this file. 2:19 Next to each package is a version number. 2:26 Notice how they only have two positions and there is a carrot at the beginning. 2:30 This defines the minimum required version and 2:34 allows composer to perform any minor or patch updates. 2:37 This also prevents composer from upgrading to the next major version. 2:42 Composer gives you many different options for controlling versions. 2:48 For example, I can change the caret to a Tilde in the HTML purifier. 2:52 I get the same results because using a tilde will increment 3:00 the last number given. 3:04 However, if I specify tilde 4.8.0, 3:06 it would only increment the patch version. 3:13 While using caret 4 .8.0 will increment both the patch and the minor version. 3:16 Check out the notes associated with this video 3:25 to learn more about options and tools. 3:28 When you make changes to your composer.json file, 3:31 those changes will not take effect until you run a composer update. 3:35
You need to sign up for Treehouse in order to download course files.Sign up