#development #laravel #php

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:

1composer require aws/aws-sdk-php
2composer 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:

1{
2    "name": "your/project-name",
3    "require": {
4        "aws/aws-sdk-php": "^3.282",
5        "google/apiclient": "^2.14"
6    }
7}

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:

 1{
 2    "require": {
 3        "aws/aws-sdk-php": "<version here>"
 4    },
 5    "scripts": {
 6        "pre-autoload-dump": "Aws\\Script\\Composer\\Composer::removeUnusedServices"
 7    },
 8    "extra": {
 9        "aws/aws-sdk-php": [
10            "Ec2",
11            "CloudWatch"
12        ]
13    }
14}

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:

 1{
 2    "require": {
 3        "google/apiclient": "^2.15.0"
 4    },
 5    "scripts": {
 6        "pre-autoload-dump": "Google\\Task\\Composer::cleanup"
 7    },
 8    "extra": {
 9        "google/apiclient-services": [
10            "Drive",
11            "YouTube"
12        ]
13    }
14}

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:

1rm -r vendor/google/apiclient-services
2composer 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:

 1{
 2    "extra": {
 3        "google/apiclient-services": [
 4            "Drive",
 5            "YouTube",
 6            "YouTubeAnalytics",
 7            "YouTubeReporting"
 8        ]
 9    }
10}

Install the depencies

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

1composer 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