PluginsService

Type
Class
Namespace
Craft
Inherits
Craft\PluginsService » Craft\BaseApplicationComponent » CApplicationComponent » CComponent
Implements
IApplicationComponent
Since
1.0

PluginsService provides APIs for managing plugins.

An instance of PluginsService is globally accessible in Craft via {@link WebApp::plugins craft()->plugins}.

See also http://craftcms.com

View source

Public Properties

PropertyDescription
$autoloadClassesarray
$behaviorsarray – The behaviors that should be attached to this component.

$autoloadClasses

Signature

public array $autoloadClasses = null

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__get()Returns a property value, an event handler list or a behavior based on its name.
__isset()Checks if a property value is null.
__set()Sets value of a component property.
__unset()Sets a component property to be null.
arePluginsLoaded()Returns whether plugins have been loaded yet for this request.
asa()Returns the named behavior object.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
attachEventHandler()Attaches an event handler to an event.
call()Calls a method on all plugins that have it, and returns an array of the results, indexed by plugin handles.
callFirst()Calls a method on the first plugin that has it, and returns the result.
callHook()Calls a method on all plugins that have the method.
canGetProperty()Determines whether a property can be read.
canSetProperty()Determines whether a property can be set.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
detachEventHandler()Detaches an existing event handler.
disableBehavior()Disables an attached behavior.
disableBehaviors()Disables all behaviors attached to this component.
disablePlugin()Disables a plugin by its handle.
doesPluginClassExist()Returns whether a plugin class exists.
doesPluginRequireDatabaseUpdate()Returns whether the given plugin’s local schema version is greater than the record we have in the database.
enableBehavior()Enables an attached behavior.
enableBehaviors()Enables all behaviors attached to this component.
enablePlugin()Enables a plugin by its handle.
evaluateExpression()Evaluates a PHP expression or callback under the context of this component.
getEventHandlers()Returns the list of attached event handlers for an event.
getIsInitialized()Checks if this application component has been initialized.
getPlugin()Returns a plugin by its handle.
getPluginClasses()Returns an array of class names found in a given plugin folder.
getPluginIconUrl()Returns a given plugin’s icon URL.
getPluginInfo()Returns the stored info for a given plugin.
getPluginLicenseKey()Returns the license key stored for a given plugin, if it was purchased through the Store.
getPluginLicenseKeyStatus()Returns the license key status of a given plugin.
getPlugins()Returns all the plugins.
hasEvent()Determines whether an event is defined.
hasEventHandler()Checks whether the named event has attached handlers.
hasPluginVersionNumberChanged()Returns whether the given plugin’s version number has changed from what we have recorded in the database.
hasProperty()Determines whether a property is defined.
init()Initializes the application component.
installPlugin()Installs a plugin by its handle.
isInitialized()Checks if this application component has been initialized yet, or not.
loadPlugins()Loads the enabled plugins.
onLoadPlugins()Fires an 'onLoadPlugins' event.
raiseEvent()Raises an event.
savePluginSettings()Saves a plugin's settings.
setPluginLicenseKey()Sets a plugin’s license key.
setPluginLicenseKeyStatus()Sets the license key status for a given plugin.
uninstallPlugin()Uninstalls a plugin by its handle.

arePluginsLoaded()

Returns whether plugins have been loaded yet for this request.

View source

Returns

boolean – Whether plugins have been loaded yet.

Signature

public boolean arePluginsLoaded ( )

call()

Calls a method on all plugins that have it, and returns an array of the results, indexed by plugin handles.

View source

Arguments

  • $method (string) – The name of the method.
  • $args (array) – Any arguments that should be passed when calling the method on the plugins.
  • $ignoreNull (boolean) – Whether plugins that have the method but return a null response should be ignored. Defaults to false.

Returns

array – An array of the plugins’ responses.

Signature

public array call ( $method, $args = [], $ignoreNull = false )

callFirst()

Calls a method on the first plugin that has it, and returns the result.

View source

Arguments

  • $method (string) – The name of the method.
  • $args (array) – Any arguments that should be passed when calling the method on the plugins.
  • $ignoreNull (boolean) – Whether plugins that have the method but return a null response should be ignored. Defaults to false.

Returns

mixed – The plugin’s response, or null.

Signature

public mixed callFirst ( $method, $args = [], $ignoreNull = false )

callHook()

DEPRECATED

Deprecated Deprecated in 1.0. Use {@link call()} instead.

Calls a method on all plugins that have the method.

View source

Arguments

  • $method (string) – The name of the method.
  • $args (array) – Any arguments that should be passed when calling the method on the plugins.

Returns

array – An array of the plugins’ responses.

Signature

public array callHook ( $method, $args = [] )

disablePlugin()

Disables a plugin by its handle.

View source

Arguments

  • $handle (string) – The plugin’s handle.

Returns

boolean – Whether the plugin was disabled successfully.

Throws

Signature

public boolean disablePlugin ( $handle )

doesPluginClassExist()

Returns whether a plugin class exists.

View source

Arguments

  • $plugin (Craft\BasePlugin) – The plugin.
  • $classSubfolder (string) – The subfolder to search.
  • $class (string) – The class suffix we’re looking for.
  • $autoload (boolean) – Whether the found class should be imported for the autoloader.

Returns

boolean – Whether the class exists.

Signature

public boolean doesPluginClassExist ( Craft\BasePlugin $plugin, $classSubfolder, $class, $autoload = true )

doesPluginRequireDatabaseUpdate()

Returns whether the given plugin’s local schema version is greater than the record we have in the database.

View source

Arguments

Returns

boolean – Whether the plugin’s local schema version is greater than the record we have in the database.

Signature

public boolean doesPluginRequireDatabaseUpdate ( Craft\BasePlugin $plugin )

enablePlugin()

Enables a plugin by its handle.

View source

Arguments

  • $handle (string) – The plugin’s handle.

Returns

boolean – Whether the plugin was enabled successfully.

Throws

Signature

public boolean enablePlugin ( $handle )

getPlugin()

Returns a plugin by its handle.

View source

Arguments

  • $handle (string) – The plugin’s handle.
  • $enabledOnly (boolean) – Whether the plugin must be installed and enabled. Defaults to true.

Returns

Craft\BasePlugin, null – The plugin.

Signature

public Craft\BasePlugin, null getPlugin ( $handle, $enabledOnly = true )

getPluginClasses()

Returns an array of class names found in a given plugin folder.

View source

Arguments

  • $plugin (Craft\BasePlugin) – The plugin.
  • $classSubfolder (string) – The subfolder to search.
  • $classSuffix (string) – The class suffix we’re looking for.
  • $autoload (boolean) – Whether the found classes should be imported for the autoloader.

Returns

array – The class names.

Signature

public array getPluginClasses ( Craft\BasePlugin $plugin, $classSubfolder, $classSuffix, $autoload = true )

getPluginIconUrl()

Returns a given plugin’s icon URL.

View source

Arguments

  • $pluginHandle (string) – The plugin’s class handle
  • $size (integer) – The size of the icon

Returns

string

Signature

public string getPluginIconUrl ( $pluginHandle, $size = 100 )

getPluginInfo()

Returns the stored info for a given plugin.

View source

Arguments

Returns

array, null – The stored info, if there is any.

Signature

public array, null getPluginInfo ( Craft\BasePlugin $plugin )

getPluginLicenseKey()

Returns the license key stored for a given plugin, if it was purchased through the Store.

View source

Arguments

  • $pluginHandle (string) – The plugin’s class handle

Returns

string, null – The plugin’s license key, or null if it isn’t known

Signature

public string, null getPluginLicenseKey ( $pluginHandle )

getPluginLicenseKeyStatus()

Returns the license key status of a given plugin.

View source

Arguments

  • $pluginHandle (string) – The plugin’s class handle

Returns

string, false

Signature

public string, false getPluginLicenseKeyStatus ( $pluginHandle )

getPlugins()

Returns all the plugins.

View source

Arguments

  • $enabledOnly (boolean) – Whether to only return plugins that are installed and enabled. Defaults to true.

Returns

Craft\BasePlugin[] – The plugins.

Signature

public Craft\BasePlugin[] getPlugins ( $enabledOnly = true )

hasPluginVersionNumberChanged()

Returns whether the given plugin’s version number has changed from what we have recorded in the database.

View source

Arguments

Returns

boolean – Whether the plugin’s version number has changed from what we have recorded in the database

Signature

public boolean hasPluginVersionNumberChanged ( Craft\BasePlugin $plugin )

installPlugin()

Installs a plugin by its handle.

View source

Arguments

  • $handle (string) – The plugin’s handle.

Returns

boolean – Whether the plugin was installed successfully.

Throws

  • \Craft\Exception|\Exception

Signature

public boolean installPlugin ( $handle )

loadPlugins()

Loads the enabled plugins.

View source

Returns

null

Signature

public null loadPlugins ( )

onLoadPlugins()

Fires an 'onLoadPlugins' event.

View source

Arguments

Returns

null

Signature

public null onLoadPlugins ( Craft\Event $event )

savePluginSettings()

Saves a plugin's settings.

View source

Arguments

Returns

boolean – Whether the plugin’s settings were saved successfully.

Signature

public boolean savePluginSettings ( Craft\BasePlugin $plugin, $settings )

setPluginLicenseKey()

Sets a plugin’s license key. Note this should not be used to store license keys generated by third party stores.

View source

Arguments

  • $pluginHandle (string) – The plugin’s class handle
  • $licenseKey (string, null) – The plugin’s license key

Returns

void

Throws

Signature

public void setPluginLicenseKey ( $pluginHandle, $licenseKey )

setPluginLicenseKeyStatus()

Sets the license key status for a given plugin.

View source

Arguments

  • $pluginHandle (string) – The plugin’s class handle
  • $licenseKeyStatus (string, null) – The plugin’s license key status

Returns

void

Signature

public void setPluginLicenseKeyStatus ( $pluginHandle, $licenseKeyStatus )

uninstallPlugin()

Uninstalls a plugin by its handle.

View source

Arguments

  • $handle (string) – The plugin’s handle.

Returns

boolean – Whether the plugin was uninstalled successfully.

Throws

  • \Craft\Exception|\Exception

Signature

public boolean uninstallPlugin ( $handle )