3. Service Class Features - modmore/Alpacka GitHub Wiki

Assuming the $service variable contains an instance of your service class that extends Alpacka, here are the features you can now use.

Context-aware Settings

When you use $modx->getOption() to get the value of a setting, it will already look through the user, user group and finally the system settings, however when used in a manager context (i.e. in a processor, many plugin events, or in a component) it will not look through the context for the current resource if there is any.

For that, Alpacka provides a $service->getOption() method, together with a $service->setResource() and $service->setWorkingContext() method.

Here's how those work.

The first thing to do, is to call $service->setResource(), passing a modResource object as the first parameter. You can do this the first time you have access to a resource, for example in plugin code after loading the service code.

The setResource method will store a local copy of the resource that can be used by other methods of the service class (e.g. path placeholders), and it will also call $service->setWorkingContext() for you with the resource context_key value. If you don't have a resource, but just a context, you can call $service->setWorkingContext() directly with the key of the context to use.

The resource and context objects are available through $service->resource and $service->context respectively.

With the context loaded, you can start using $service->getOption() instead of $modx->getOption(). They have the same signature/parameters in MODX 2.x, so you can easily switch them over. As a reminder, these are the parameters in order:

  • $key: the name of the setting to look up
  • $options: either an array to search through first (e.g. the properties passed to a snippet), or null to only look through settings.
  • $default: a default value to use if
  • $skipEmpty: defaults to false, but set to true to use the $default when a value was set in the settings or $options array, but it was empty.

If no working context is set on the service class, $service->getOption() works exactly as $modx->getOption().

Another setting-related method provided by Alpacka is $service->getBooleanOption(). As the name might give away, this works exactly like $service->getOption() with the exception that the value is always cast to a boolean. Aside from the actual booleans, integer 1/0 and string 1/0, this method also casts string false and string no to a boolean false for convenience. This uses the $service->castValueToBoolean() method. See (https://github.com/modmore/Alpacka/blob/master/tests/ServiceTest.php#L87)[the tests] for how values are cast to a boolean.

Path Placeholders

Loading xPDO Package automatically

Loading default lexicon automatically

Loading settings by namespace into $config

Creating Version object

Pimple Dependency Injection Container

Parsing Chunks

Sanitising files/folder names

explode() + trim convenience method

getUltimateParent convenience method