PHP Class Vanilla\AddonManager

The {@link AddonManager} scans directories for addon folders and then maintains them in a catalogue. Addons can then be started which makes them available to the application. When an addon is started it can do the following: - Any classes the addon has declared are available via the {@link AddonManager::autoload()} method. - The addon can declare a class ending in "Plugin" and its events will be registered (TODO). - Any translations the addon has declared will be loaded for the currently enabled locale.
Exibir arquivo Open project: vanilla/vanilla Class Usage Examples

Public Methods

Method Description
__construct ( array $scanDirs, string $cacheDir ) Initialize a new instance of the {@link AddonManager} class.
autoload ( string $class ) Attempt to load undefined class based on the addons that are enabled.
checkDependants ( Addon $addon, boolean $throw = false ) : boolean Check the enabled dependants of an addon.
checkRequirements ( Addon $addon, boolean $throw = false ) : boolean Check an addon's requirements.
clearCache ( ) : boolean Remove all of the cached files.
getCacheDir ( ) : string Get the cacheDir.
getEnabled ( ) : array[Addon] Get the enabled addons, sorted by priority with the highest priority first.
getEnabledTranslationPaths ( string $locale ) : array Get the paths to the current translation files.
getTheme ( ) : Addon | null Get the theme.
isEnabled ( string $key, string $type ) : boolean Check whether or not an addon is enabled.
lookupAddon ( string $key ) : Addon | null Lookup the addon with a given key.
lookupAllByType ( string $type ) : array Get all of the addons of a certain type.
lookupAsset ( string $subpath, Addon $addon = null, boolean $mustExist = true ) : string Lookup the path of an asset.
lookupByClassname ( string $class, boolean $searchAll = false ) : Addon | null Lookup an addon by class name.
lookupByType ( string $key, string $type ) : null | Addon Lookup an {@link Addon} by its type.
lookupDependants ( Addon $addon ) : array Get all of the enabled addons that depend on a given addon.
lookupLocale ( string $key ) : null | Addon Lookup a locale pack based on its key.
lookupRequirements ( Addon $addon, integer $filter = null ) : Returns Get all of the requirements for an addon.
lookupTheme ( string $key ) : null | Addon Lookup a theme based on its key.
scan ( string $type, boolean $saveCache = false ) : array Scan the directories of all of the addons of a given type.
setCacheDir ( string $cacheDir ) : AddonManager Set the cacheDir.
setTheme ( Addon $theme ) : AddonManager Set the theme.
startAddon ( Addon $addon ) Start an addon and make it available.
startAddonsByKey ( array $keys, string $type ) : integer Start one or more addons by specifying their keys.
stopAddon ( Addon $addon ) Stop an addon and make it unavailable.
stopAddonsByKey ( array $keys, string $type ) : integer Stop one or more addons by specifying their keys.

Private Methods

Method Description
deleteSingleIndexKey ( string $type, string $key ) : boolean Delete an item from a single index and re-cache it.
ensureMultiCache ( ) Ensure that the addon cache has all of the addons.
filePutContents ( string $filename, mixed $data, integer $mode = 420 ) : boolean A version of file_put_contents() that is multi-thread safe.
getSingleIndex ( string $type ) : array Get the index for an addon type that is cached by single addon.
lookupRequirementsRecursive ( Addon $addon, array &$array ) The implementation of {@link lookupRequirements()}.
lookupSingleCachedAddon ( string $key, string $type ) : Addon | null Lookup an addon that is cached on a per-addon basis.
saveArrayCache ( string $path, string $array ) Cache an array.
scanAddonDirs ( string $type ) : array Get a list of addon directories for a given type.
themeSubdirs ( ) : array Get the current theme and themes it's based on as an array.
typeUsesMultiCaching ( string $type ) : boolean Test whether an addon type uses multi-caching.

Method Details

__construct() public method

Initialize a new instance of the {@link AddonManager} class.
public __construct ( array $scanDirs, string $cacheDir )
$scanDirs array An array of root-relative directories to scan indexed by **Addon::TYPE_*** constant. Applications and plugins are treated as the same so pass their directories as an array with the **Addon::TYPE_ADDON** key.
$cacheDir string The path to the cache.

autoload() public method

Attempt to load undefined class based on the addons that are enabled.
public autoload ( string $class )
$class string The name of the class to load.

checkDependants() public method

Addons should always check their dependants before being disabled. This check does not consider dependants that are not enabled.
public checkDependants ( Addon $addon, boolean $throw = false ) : boolean
$addon Addon The addon to check.
$throw boolean Whether or not to throw an exception or just return **false** if the check fails.
return boolean Returns **true** if the addon a

checkRequirements() public method

An addon cannot be enabled if it has missing or invalid requirements. If an addon has requirements that are simply disabled it will pass this test as long as it's requirements also meet *their* requirements.
public checkRequirements ( Addon $addon, boolean $throw = false ) : boolean
$addon Addon The addon to check.
$throw boolean Whether or not to throw an exception if the requirements are not met.
return boolean Returns **true** if the requirements are met or **false** otherwise.

clearCache() public method

Remove all of the cached files.
public clearCache ( ) : boolean
return boolean Returns **true** if the files were removed or **false** otherwise.

getCacheDir() public method

Get the cacheDir.
public getCacheDir ( ) : string
return string Returns the cacheDir.

getEnabled() public method

Get the enabled addons, sorted by priority with the highest priority first.
public getEnabled ( ) : array[Addon]
return array[Addon]

getEnabledTranslationPaths() public method

Get the paths to the current translation files.
public getEnabledTranslationPaths ( string $locale ) : array
$locale string The locale to get the translation paths for.
return array Returns an array of paths.

getTheme() public method

Get the theme.
public getTheme ( ) : Addon | null
return Addon | null Returns the theme.

isEnabled() public method

Check whether or not an addon is enabled.
public isEnabled ( string $key, string $type ) : boolean
$key string The addon key.
$type string One of the **Addon::TYPE_*** constants.
return boolean Returns

lookupAddon() public method

Lookup the addon with a given key.
public lookupAddon ( string $key ) : Addon | null
$key string The key of the addon.
return Addon | null

lookupAllByType() public method

Get all of the addons of a certain type.
public lookupAllByType ( string $type ) : array
$type string One of the **Addon::TYPE_*** constants.
return array

lookupAsset() public method

Lookup the path of an asset.
public lookupAsset ( string $subpath, Addon $addon = null, boolean $mustExist = true ) : string
$subpath string The subpath of the asset, relative an addon root.
$addon Addon The addon that should contain the asset.
$mustExist boolean Whether or not the asset must exist in the addon.
return string

lookupByClassname() public method

This method should only be used with enabled addons as searching through all addons takes a performance hit.
public lookupByClassname ( string $class, boolean $searchAll = false ) : Addon | null
$class string The class name.
$searchAll boolean Whether or not to search all addons or just the enabled ones.
return Addon | null Returns an {@link Addon} object or **null** if one isn't found.

lookupByType() public method

Lookup an {@link Addon} by its type.
public lookupByType ( string $key, string $type ) : null | Addon
$key string The addon's key.
$type string One of the **Addon::TYPE_*** constants.
return null | Addon Returns the addon or **null** if one isn't found.

lookupDependants() public method

Get all of the enabled addons that depend on a given addon.
public lookupDependants ( Addon $addon ) : array
$addon Addon The addon to check the requirements.
return array Returns an array of {@link Addon} objects.

lookupLocale() public method

The local pack's key MUST be the same as the folder it's in.
public lookupLocale ( string $key ) : null | Addon
$key string The key of the locale pack.
return null | Addon Returns an {@link Addon} object for the locale pack or **null** if it can't be found.

lookupRequirements() public method

This method returns an array of all of the addon requirements for a given addon. The return is an array of requirements in the following form: 'addonKey' => ['req' => 'versionRequirement', 'status' => AddonManager::REQ_*]
public lookupRequirements ( Addon $addon, integer $filter = null ) : Returns
$addon Addon The addon to check.
$filter integer One or more of the **AddonManager::REQ_*** constants concatenated by `|`.
return Returns the requirements array. An empty array represents an addon with no requirements.

lookupTheme() public method

The theme's key MUST be the same as the folder it's in.
public lookupTheme ( string $key ) : null | Addon
$key string The key of the theme.
return null | Addon Returns an {@link Addon} object for the theme or **null** if it can't be found.

scan() public method

Scan the directories of all of the addons of a given type.
public scan ( string $type, boolean $saveCache = false ) : array
$type string One of the **Addon::TYPE_*** constants.
$saveCache boolean Whether or not to save the found addons to the cache.
return array Returns an array of {@link Addon} objects.

setCacheDir() public method

Set the cacheDir.
public setCacheDir ( string $cacheDir ) : AddonManager
$cacheDir string The cache directory to set. If this doesn't include **PATH_ROOT** then it will be prepended.
return AddonManager Returns `$this` for fluent calls.

setTheme() public method

Set the theme.
public setTheme ( Addon $theme ) : AddonManager
$theme Addon The new theme to set.
return AddonManager Returns `$this` for fluent calls.

startAddon() public method

Start an addon and make it available.
public startAddon ( Addon $addon )
$addon Addon The addon to start.

startAddonsByKey() public method

This method is useful for starting the addons that are stored in a configuration file.
public startAddonsByKey ( array $keys, string $type ) : integer
$keys array The keys of the addons. The addon keys can be the keys of the array or the values.
$type string One of the **Addon::TYPE_*** constants.
return integer Returns the number of addons that were enabled.

stopAddon() public method

Stop an addon and make it unavailable.
public stopAddon ( Addon $addon )
$addon Addon The addon to stop.

stopAddonsByKey() public method

Stop one or more addons by specifying their keys.
public stopAddonsByKey ( array $keys, string $type ) : integer
$keys array The keys of the addons. The addon keys can be the keys of the array or the values.
$type string One of the **Addon::TYPE_*** constants.
return integer Returns the number of addons that were stopped.