You are here

class AuthcacheP13nSettingBuilder in Authenticated User Page Caching (Authcache) 7.2

Content builder for personalized Drupal.settings.

Hierarchy

Expanded class hierarchy of AuthcacheP13nSettingBuilder

2 string references to 'AuthcacheP13nSettingBuilder'
AuthcacheP13nTestRequestBuilder::testRequestResourcesNullFragments in modules/authcache_p13n/tests/authcache_p13n.request-builder.test
Cover authcache_p13n_request_resources().
authcache_p13n_authcache_p13n_request in modules/authcache_p13n/authcache_p13n.module
Implements hook_authcache_p13n_request().

File

modules/authcache_p13n/includes/AuthcacheP13nSettingBuilder.inc, line 10
Defines AuthcacheP13nSettingBuilder.

View source
class AuthcacheP13nSettingBuilder implements AuthcacheP13nContentBuilderInterface, AuthcacheP13nRequestValidatorInterface {

  /**
   * A list of settings keyed by parameter name.
   */
  protected $settings;

  /**
   * Construct new instance.
   *
   * @param array $settings
   *   List of key-value pairs where the key is a setting-id and the value a
   *   record with the following entries:
   *   - renderer: A AuthcacheP13nSetting instance
   *   - target: A string specifying the target Drupal.setting key.
   *   - validator: (optional) an AuthcacheP13nSettingValidator instance
   *   - access: (optional) an AuthcacheP13nSettingAccess instance
   */
  public function __construct($settings) {
    $this->settings = $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function validate($req) {

    // Run setting request validators.
    if (!empty($req['a']) && is_array($req['a'])) {
      foreach ($req['a'] as $paramname => $params) {
        if (!empty($this->settings[$paramname]['validator'])) {
          $req['a'][$paramname] = $this->settings[$paramname]['validator']
            ->validate($params);
        }
      }
    }
    return $req;
  }

  /**
   * {@inheritdoc}
   */
  public function build($req, $context) {
    global $user;
    $data = array();
    foreach ($this->settings as $paramname => $setting) {
      $params = isset($req['a'][$paramname]) ? $req['a'][$paramname] : NULL;

      // Run access check.
      if (!empty($setting['access']) && !$setting['access']
        ->check($user, $params, $context)) {
        throw new AuthcacheP13nRequestAccessDenied();
      }

      // Render result.
      $data[] = array(
        $setting['target'] => $setting['renderer']
          ->get($params, $context),
      );
    }

    // Merge and return result of all settings.
    return drupal_array_merge_deep_array($data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthcacheP13nSettingBuilder::$settings protected property A list of settings keyed by parameter name.
AuthcacheP13nSettingBuilder::build public function Build and return the content defined by the input parameters. Overrides AuthcacheP13nContentBuilderInterface::build
AuthcacheP13nSettingBuilder::validate public function Validate and sanitize a request. Overrides AuthcacheP13nRequestValidatorInterface::validate
AuthcacheP13nSettingBuilder::__construct public function Construct new instance.