When working with PHP applications that require AWS and Google Services integration, it's essential to manage dependencies efficiently. Using Composer, a dependency management tool for PHP, you can streamline the installation process and ensure that only the required services are installed. In this blog post, we will guide you through the process of setting up Composer to install the AWS PHP SDK and the Google APIs Client Library for PHP packages in an optimised way.

Install the packages

In this example, we will install the AWS SDK for PHP and Google APIs Client Library for PHP packages.

To do this, run the following command:

composer require aws/aws-sdk-php
composer require google/apiclient

Editing the composer.json File

Next, open your project's composer.json file in a text editor. This file is where you define your project's dependencies and settings. Here's an example of what it might look like:

{
    "name": "your/project-name",
    "require": {
        "aws/aws-sdk-php": "^3.282",
        "google/apiclient": "^2.14"
    }
}

Removing unused AWS dependencies

To avoid shipping unused services, specify which services you would like to keep in your composer.json file and use the Aws\\Script\\Composer::removeUnusedServices script:

{
    "require": {
        "aws/aws-sdk-php": "<version here>"
    },
    "scripts": {
        "pre-autoload-dump": "Aws\\Script\\Composer\\Composer::removeUnusedServices"
    },
    "extra": {
        "aws/aws-sdk-php": [
            "Ec2",
            "CloudWatch"
        ]
    }
}

In this example, all services deemed safe for deletion will be removed except for Ec2 and CloudWatch. When listing a service, keep in mind that an exact match is needed on the client namespace, otherwise, an error will be thrown. For a list of client namespaces, please see the Namespaces list in the documentation. Run composer install or composer update to start service removal.

NOTE: S3, Kms, SSO and Sts are used by core SDK functionality and thus are unsafe for deletion. They are excluded from deletion in this script. If you accidentally remove a service you'd like to keep, you will need to reinstall the SDK. We suggest using composer reinstall aws/aws-sdk-php.

Removing unused Google dependencies

There are over 200 Google API services. The chances are good that you will not want them all. In order to avoid shipping these dependencies with your code, you can run the Google\Task\Composer::cleanup task and specify the services you want to keep in composer.json:

{
    "require": {
        "google/apiclient": "^2.15.0"
    },
    "scripts": {
        "pre-autoload-dump": "Google\\Task\\Composer::cleanup"
    },
    "extra": {
        "google/apiclient-services": [
            "Drive",
            "YouTube"
        ]
    }
}

This example will remove all services other than "Drive" and "YouTube" when composer update or a fresh composer install is run.

IMPORTANT If you add any services back in composer.json, you will need to remove the vendor/google/apiclient-services directory explicitly for the change you made to have effect:

rm -r vendor/google/apiclient-services
composer update

NOTE This command performs an exact match on the service name, so to keep YouTubeReporting and YouTubeAnalytics as well, you'd need to add each of them explicitly:

{
    "extra": {
        "google/apiclient-services": [
            "Drive",
            "YouTube",
            "YouTubeAnalytics",
            "YouTubeReporting"
        ]
    }
}

Install the depencies

After editing the composer.json file, run the following command to install the dependencies:

composer install

Composer will download and install the specified packages, including their dependencies, into the vendor directory of your project.

Conclusion

Using Composer to manage AWS and Google Services dependencies in your PHP project is a best practice for efficient development. By specifying only the required packages, you can keep your application lightweight, reduce potential conflicts, and make it easier to maintain.

Resources