PHP Class PhilippBaschke\ACFProInstaller\Plugin

The WordPress plugin Advanced Custom Fields PRO (ACF PRO) does not offer a way to install it via composer natively. This plugin uses a 'package' repository (user supplied) that downloads the correct version from the ACF site using the version number from that repository and a license key from the ENVIRONMENT or an .env file. With this plugin user no longer need to expose their license key in composer.json.
Inheritance: implements Composer\Plugin\PluginInterface, implements Composer\EventDispatcher\EventSubscriberInterface
Datei anzeigen Open project: philippbaschke/acf-pro-installer Class Usage Examples

Protected Properties

Property Type Description
$composer Composer\Composer
$io Composer\IO\IOInterface

Public Methods

Method Description
activate ( Composer\Composer $composer, Composer\IO\IOInterface $io ) The function that is called when the plugin is activated
addKey ( Composer\Plugin\PreFileDownloadEvent $event ) Add the key from the environment to the event url
addVersion ( Composer\Installer\PackageEvent $event ) Add the version to the package url
getSubscribedEvents ( ) : array Subscribe this Plugin to relevant Events

Protected Methods

Method Description
addParameterToUrl ( string $url, string $parameter, string $value ) : string Add a parameter to the given url
getKeyFromEnv ( ) : string Get the ACF PRO key from the environment
getPackageFromOperation ( Composer\DependencyResolver\Operation\OperationInterface $operation ) : PackageInterface Get the package from a given operation
isAcfProPackageUrl ( $url ) : boolean Test if the given url is the ACF PRO download url
loadDotEnv ( ) Make environment variables in .env available if .env exists
removeParameterFromUrl ( string $url, string $parameter ) : string Remove a given parameter from the given url
validateVersion ( string $version ) : string Validate that the version is an exact major.minor.patch.optional version

Method Details

activate() public method

Makes composer and io available because they are needed in the addKey method.
public activate ( Composer\Composer $composer, Composer\IO\IOInterface $io )
$composer Composer\Composer The composer object
$io Composer\IO\IOInterface Not used

addKey() public method

The key is not added to the package because it would show up in the composer.lock file in this case. A custom file system is used to swap out the ACF PRO url with a url that contains the key.
public addKey ( Composer\Plugin\PreFileDownloadEvent $event )
$event Composer\Plugin\PreFileDownloadEvent The event that called this method

addParameterToUrl() protected method

Adds the given parameter at the end of the given url. It only works with urls that already have parameters (e.g. test.com?p=true) because it uses & as a separation character.
protected addParameterToUrl ( string $url, string $parameter, string $value ) : string
$url string The url that should be appended
$parameter string The name of the parameter
$value string The value of the parameter
return string The url appended with ¶meter=value

addVersion() public method

The version needs to be added in the PRE_PACKAGE_INSTALL/UPDATE event to make sure that different version save different urls in composer.lock. Composer would load any available version from cache although the version numbers might differ (because they have the same url).
public addVersion ( Composer\Installer\PackageEvent $event )
$event Composer\Installer\PackageEvent The event that called the method

getKeyFromEnv() protected method

Loads the .env file that is in the same directory as composer.json and gets the key from the environment variable KEY_ENV_VARIABLE. Already set variables will not be overwritten by the variables in .env
protected getKeyFromEnv ( ) : string
return string The key from the environment

getPackageFromOperation() protected method

Is needed because update operations don't have a getPackage method
protected getPackageFromOperation ( Composer\DependencyResolver\Operation\OperationInterface $operation ) : PackageInterface
$operation Composer\DependencyResolver\Operation\OperationInterface The operation
return PackageInterface The package of the operation

getSubscribedEvents() public static method

Pre Install/Update: The version needs to be added to the url (will show up in composer.lock) Pre Download: The key needs to be added to the url (will not show up in composer.lock)
public static getSubscribedEvents ( ) : array
return array An array of events that the plugin subscribes to

isAcfProPackageUrl() protected method

Test if the given url is the ACF PRO download url
protected isAcfProPackageUrl ( $url ) : boolean
return boolean

loadDotEnv() protected method

getcwd() returns the directory of composer.json.
protected loadDotEnv ( )

removeParameterFromUrl() protected method

Removes ¶meter=value from the given url. Only works with urls that have multiple parameters and the parameter that should be removed is not the first (because of the & character).
protected removeParameterFromUrl ( string $url, string $parameter ) : string
$url string The url where the parameter should be removed
$parameter string The name of the parameter
return string The url with the ¶meter=value removed

validateVersion() protected method

The url to download the code for the package only works with exact version numbers with 3 or 4 digits: e.g. 1.2.3 or 1.2.3.4
protected validateVersion ( string $version ) : string
$version string The version that should be validated
return string The valid version

Property Details

$composer protected_oe property

protected Composer,Composer $composer
return Composer\Composer

$io protected_oe property

protected IOInterface,Composer\IO $io
return Composer\IO\IOInterface