Skip to content

Improve composer version defaults #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
## {{ UNRELEASED_VERSION }} - [{{ UNRELEASED_DATE }}]({{ UNRELEASED_LINK }})

* Fixed mismatched `libsqlite3-dev` and `libsqlite3-0` versions in PHP 8.3 and 8.4 images.
* Added logic to allow default `composer` version to be set based on PHP version.
* Added `2.2` and `2.2-latest` shorthand options to install the latest stable 2.2 LTS version of `composer`.
* Set default `composer` version to `2-latest`
* Set default `composer` version to `2.2-latest` for PHP 5.3-7.2
* Set default `composer` version to `1-latest` for PHP <= 5.2
* Removed `composer` installation from images to prefer installing during app build
* Fixed bug causing `composer` 2.2.x to be installed when `composer_version` was set to a single digit version such as `1`
* Fixed mismatched `libsqlite3-dev` and `libsqlite3-0` versions in PHP 8.3 and 8.4 images

## v1.6.4 - [December 14, 2024](https://github.com/lando/php/releases/tag/v1.6.4)

Expand Down
87 changes: 73 additions & 14 deletions builders/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ const _ = require('lodash');
const path = require('path');
const semver = require('semver');
const addBuildStep = require('./../utils/add-build-step');

/**
* Get the appropriate Composer version based on the PHP version.
* @param {semver} phpSemver - The PHP semantic version.
* @return {string|boolean} - The Composer version or false if we cannot parse the version.
*/
const getDefaultComposerVersion = phpSemver => {
// Don't set a default composer version if we cannot
// parse the version such as with `custom`.
if (!phpSemver) return false;

if (semver.lt(phpSemver, '5.3.2')) {
// Use Composer 1 for PHP < 5.3.2
return '1';
} else if (semver.lt(phpSemver, '7.3.0')) {
// Use Composer 2.2 LTS for PHP < 7.3
return '2.2';
} else {
// Use Composer 2 for PHP >= 7.3
return '2';
}
};

/*
* Helper to get nginx config
*/
Expand All @@ -30,13 +53,25 @@ const nginxConfig = options => ({
const xdebugConfig = host => ([
`client_host=${host}`,
'discover_client_host=1',
'log=/tmp/xdebug.log',
'remote_enable=true',
'log=/tmp/xdebug.log',
'remote_enable=true',
`remote_host=${host}`,
].join(' '));

/*
* Helper to build a package string
/**
* Helper function to build a package string by combining package name and version
*
* @param {string} pkg - The package name
* @param {string} version - The package version
* @return {string} The formatted package string, either "pkg:version" or just "pkg" if version is empty
*
* @example
* // Returns "php:7.4"
* pkger('php', '7.4');
*
* @example
* // Returns "mysql"
* pkger('mysql', '');
*/
const pkger = (pkg, version) => (!_.isEmpty(version)) ? `${pkg}:${version}` : pkg;

Expand Down Expand Up @@ -108,7 +143,7 @@ module.exports = {
],
confSrc: path.resolve(__dirname, '..', 'config'),
command: ['sh -c \'a2enmod rewrite && apache2-foreground\''],
composer_version: '2.2.22',
composer_version: true,
phpServer: 'apache',
defaultFiles: {
_php: 'php.ini',
Expand Down Expand Up @@ -137,12 +172,20 @@ module.exports = {
parent: '_appserver',
builder: (parent, config) => class LandoPhp extends parent {
constructor(id, options = {}, factory) {
const debug = _.get(options, '_app._lando').log.debug;

// Merge the user config onto the default options
options = parseConfig(_.merge({}, config, options));

// Get the semver of the PHP version, NULL if we cannot parse it
const phpSemver = semver.coerce(options.version);
phpSemver && debug('Parsed PHP semantic version: %s', phpSemver);

// Mount our default php config
options.volumes.push(`${options.confDest}/${options.defaultFiles._php}:${options.remoteFiles._php}`);
options.volumes.push(`${options.confDest}/${options.defaultFiles.pool}:${options.remoteFiles.pool}`);
// Shift on the docker entrypoint if this is a more recent version
if (options.version !== 'custom' && semver.gt(semver.coerce(options.version), '5.5.0')) {
if (phpSemver && semver.gt(phpSemver, '5.5.0')) {
options.command.unshift('docker-php-entrypoint');
}

Expand All @@ -168,22 +211,38 @@ module.exports = {
};
options.info = {via: options.via};

// Add our composer things to run step
if (!_.isEmpty(options.composer)) {
const commands =
require('../utils/get-install-commands')(options.composer, pkger, ['composer', 'global', 'require', '-n']);
addBuildStep(commands, options._app, options.name, 'build_internal');
// Determine the appropriate composer version to install if not specified
if (options.composer_version === true || options.composer_version === '') {
options.composer_version = getDefaultComposerVersion(phpSemver);
} else if (typeof options.composer_version === 'number') {
options.composer_version = options.composer_version.toString();
}
const usingComposer1 = options.composer_version && semver.satisfies(options.composer_version, '1.x');

// Add activate steps for xdebug
// Add prestissimo as a global package for Composer 1.x performance improvements. Requires PHP >= 5.3
if (usingComposer1 && phpSemver && semver.gte(phpSemver, '5.3.0')) {
options.composer = options.composer || {};
options.composer = {'hirak/prestissimo': '*', ...options.composer};
}

// Add build step to enable xdebug
if (options.xdebug) {
addBuildStep(['docker-php-ext-enable xdebug'], options._app, options.name, 'build_as_root_internal');
}

// Install the desired composer version
// Add build step to install our Composer global packages
if (!_.isEmpty(options.composer)) {
const commands =
require('../utils/get-install-commands')(options.composer, pkger, ['composer', 'global', 'require', '-n']);
addBuildStep(commands, options._app, options.name, 'build_internal');
}

// Install the desired composer version as the first `build_internal` build step
if (options.composer_version) {
debug('Installing composer version %s', options.composer_version);
const commands = [`/helpers/install-composer.sh ${options.composer_version}`];
addBuildStep(commands, options._app, options.name, 'build_internal', true);
const firstStep = true;
addBuildStep(commands, options._app, options.name, 'build_internal', firstStep);
}

// Add in nginx if we need to
Expand Down
22 changes: 17 additions & 5 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ services:
webroot: .
xdebug: false
composer: []
composer_version: '2.2.12'
composer_version: '2'
# Below only valid for via: cli
command: tail -f /dev/null
config:
Expand Down Expand Up @@ -161,16 +161,22 @@ You can use `lando info --deep | grep IPAddress` to help discover the correct ho

## Installing composer

As of Lando `3.0.17` you can configure the version of `composer` you would like to install. This _should_ respect any of the versions listed on the [Composer download page](https://getcomposer.org/download/) but it is required you specify down to the patch version.
Lando automatically installs the latest compatible version of Composer based on your specified PHP version:

- PHP >= 7.3: Composer 2.x
- PHP >= 5.3.2 and < 7.3: Composer 2.2 LTS
- PHP < 5.3.2: Composer 1.x

You can customize the Composer version by specifying either a specific version number or using a channel alias:

```yaml
services:
myservice:
type: php
composer_version: "1.10.1"
type: php:8.2
composer_version: "2.6.5" # Install specific version
```

You can also choose to ignore the `composer` install step by setting `composer_version: false`. This will use whatever version of `composer` was last bundled with our `php` image. The following "convenience flags" are also available:
The following channel aliases are available:

```yaml
# Install the latest stable 1.x version
Expand All @@ -181,13 +187,19 @@ composer_version: 1-latest
composer_version: 2
composer_version: 2-latest

# Install the latest stable 2.2 LTS version
composer_version: 2.2
composer_version: 2.2-latest

# Install latest pre-release version
composer_version: preview

# Install latest commit
composer_version: snapshot
```

You can disable Composer installation entirely by setting `composer_version: false`.

## Installing global dependencies

You can also use the `composer` key if you need to require any [global composer dependenices](https://getcomposer.org/doc/03-cli.md#require). This follows the same syntax as your normal [`composer.json`](https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup) except written as YAML instead of JSON.
Expand Down
5 changes: 2 additions & 3 deletions examples/5.6/.lando.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ events:
services:
defaults:
type: php:5.6
composer_version: false
cli:
type: php:5.6
composer_version: false
composer_version: 1
via: cli
cliworker:
type: php:5.6
Expand All @@ -17,7 +16,7 @@ services:
command: sleep infinity
custom:
type: php:5.6
composer_version: false
composer_version: true
via: nginx
ssl: true
webroot: web
Expand Down
14 changes: 7 additions & 7 deletions examples/5.6/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ lando exec defaults -- curl http://localhost | grep "memory_limit" | grep "1G"
# Should have COMPOSER_MEMORY_LIMIT set to -1
lando exec defaults -- env | grep "COMPOSER_MEMORY_LIMIT=-1"

# Should install composer 1.x by default
lando exec defaults -- composer --version --no-ansi | grep "Composer version 1."
# Should install composer 2.2.x by default
lando exec defaults -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 2.2."

# Should have unlimited memory for php for CLI opts
lando php -i | grep memory_limit | grep -e "-1"
Expand All @@ -61,8 +61,8 @@ lando exec custom_nginx -- curl https://localhost | grep SERVER | grep PATH_TRAN
# Should use specified php version if given
lando exec custom -- php -v | grep "PHP 5.6"

# Should install composer 1.x if composer_version is set to true
lando exec custom -- composer --version --no-ansi | grep "Composer version 1."
# Should install composer 2.2.x if composer_version is set to true
lando exec custom -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 2.2."

# Should serve via nginx if specified
lando exec custom_nginx -- curl http://localhost | grep "WEBDIR"
Expand All @@ -77,7 +77,7 @@ lando exec custom -- php -m | grep "xdebug"
lando exec cli -- curl http://localhost || echo $? | grep 7

# Should install the latest composer 1.x using the 1 flag
lando exec cli -- composer --version --no-ansi | grep "Composer version 1."
lando exec cli -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 1."

# Should use custom php ini if specified
lando exec custom -- php -i | grep memory_limit | grep 514
Expand Down Expand Up @@ -112,8 +112,8 @@ lando exec defaults -- curl http://localhost/path_info.php/a/b.php | grep SCRIPT
# Should allow cli services to specify a boot up command
lando info -s cliworker --deep | grep Cmd | grep sleep | grep infinity

# Should install the latest composer 1.x by default.
lando exec cliworker -- composer --version --no-ansi | grep "Composer version 1."
# Should use preinstalled composer 1.x when composer_version is false
lando exec cliworker -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 1."
```

## Destroy tests
Expand Down
3 changes: 1 addition & 2 deletions examples/7.0/.lando.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ events:
services:
defaults:
type: php:7.0
composer_version: false
cli:
type: php:7.0
composer_version: false
composer_version: 1
via: cli
build_as_root:
- echo "deb http://archive.debian.org/debian stretch main" > /etc/apt/sources.list
Expand Down
12 changes: 6 additions & 6 deletions examples/7.0/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ lando exec defaults -- curl http://localhost | grep "memory_limit" | grep "1G"
# Should have COMPOSER_MEMORY_LIMIT set to -1
lando exec defaults -- env | grep "COMPOSER_MEMORY_LIMIT=-1"

# Should install composer 1.x by default
lando exec defaults -- composer --version --no-ansi | grep "Composer version 1."
# Should install composer 2.2.x by default
lando exec defaults -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 2.2."

# Should have unlimited memory for php for CLI opts
lando php -i | grep memory_limit | grep -e "-1"
Expand All @@ -63,7 +63,7 @@ lando exec custom_nginx -- curl https://localhost | grep SERVER | grep PATH_TRAN
lando exec custom -- php -v | grep "PHP 7.0"

# Should install composer 2.x if 2-latest is set
lando exec custom -- composer --version --no-ansi | grep "Composer version 2."
lando exec custom -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 2."

# Should serve via nginx if specified
lando exec custom_nginx -- curl http://localhost | grep "WEBDIR"
Expand All @@ -78,7 +78,7 @@ lando exec custom -- php -m | grep "xdebug"
lando exec cli -- curl http://localhost || echo $? | grep 7

# Should install the latest composer 1.x using the 1 flag
lando exec cli -- composer --version --no-ansi | grep "Composer version 1."
lando exec cli -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 1."

# Should use custom php ini if specified
lando exec custom -- php -i | grep memory_limit | grep 514
Expand Down Expand Up @@ -113,8 +113,8 @@ lando exec defaults -- curl http://localhost/path_info.php/a/b.php | grep SCRIPT
# Should allow cli services to specify a boot up command
lando info -s cliworker --deep | grep Cmd | grep sleep | grep infinity

# Should install the latest composer 1.x by default.
lando exec cliworker -- composer --version --no-ansi | grep "Composer version 1."
# Should install the latest composer 1.x when composer_version is false
lando exec cliworker -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 1."

# Should have node14 installed in cli service
lando node -v | grep v14.
Expand Down
3 changes: 1 addition & 2 deletions examples/7.1/.lando.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ events:
services:
defaults:
type: php:7.1
composer_version: false
cli:
type: php:7.1
composer_version: false
composer_version: 1
via: cli
build_as_root:
- curl -sL https://deb.nodesource.com/setup_14.x | bash -
Expand Down
12 changes: 6 additions & 6 deletions examples/7.1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ lando exec defaults -- curl http://localhost | grep "memory_limit" | grep "1G"
# Should have COMPOSER_MEMORY_LIMIT set to -1
lando exec defaults -- env | grep "COMPOSER_MEMORY_LIMIT=-1"

# Should install composer 1.x by default
lando exec defaults -- composer --version --no-ansi | grep "Composer version 1."
# Should install composer 2.2.x by default
lando exec defaults -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 2.2."

# Should have unlimited memory for php for CLI opts
lando php -i | grep memory_limit | grep -e "-1"
Expand All @@ -63,7 +63,7 @@ lando exec custom_nginx -- curl https://localhost | grep SERVER | grep PATH_TRAN
lando exec custom -- php -v | grep "PHP 7.1"

# Should install composer 2.x if 2-latest is set
lando exec custom -- composer --version --no-ansi | grep "Composer version 2."
lando exec custom -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 2."

# Should serve via nginx if specified
lando exec custom_nginx -- curl http://localhost | grep "WEBDIR"
Expand All @@ -78,7 +78,7 @@ lando exec custom -- php -m | grep "xdebug"
lando exec cli -- curl http://localhost || echo $? | grep 7

# Should install the latest composer 1.x using the 1 flag
lando exec cli -- composer --version --no-ansi | grep "Composer version 1."
lando exec cli -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 1."

# Should use custom php ini if specified
lando exec custom -- php -i | grep memory_limit | grep 514
Expand Down Expand Up @@ -113,8 +113,8 @@ lando exec defaults -- curl http://localhost/path_info.php/a/b.php | grep SCRIPT
# Should allow cli services to specify a boot up command
lando info -s cliworker --deep | grep Cmd | grep sleep | grep infinity

# Should install the latest composer 1.x by default.
lando exec cliworker -- composer --version --no-ansi | grep "Composer version 1."
# Should install the latest composer 1.x if composer_version is set to false
lando exec cliworker -- composer --version --no-ansi | tee >(cat 1>&2) | grep -q "Composer version 1."

# Should have node14 installed in cli service
lando node -v | grep v14.
Expand Down
3 changes: 1 addition & 2 deletions examples/7.2/.lando.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ events:
services:
defaults:
type: php:7.2
composer_version: false
cli:
type: php:7.2
composer_version: false
composer_version: 1
via: cli
build_as_root:
- curl -sL https://deb.nodesource.com/setup_14.x | bash -
Expand Down
Loading
Loading