Victopia / php-node

Easy data framework for PHP MySQL, same query method for both physical and virtual columns.
Do What The F*ck You Want To Public License
1 stars 0 forks source link

Port resolver chain into configuration #12

Open vicary opened 9 years ago

vicary commented 9 years ago

This is originally meant to port the whole resolver chain into configuration, but doesn't work because some options are taken from other configuration paths.

Need to come up with a referencing mechanism of a config value from one another, along with expressions and default values.

But then creating too much custom syntax just ruined the whole idea.

  $resolvers = conf::get('web::resolvers');
  array_walk($resolvers, function($options) use($resolver) {
    if ( empty($options['enable']) || $options['enable'] ) {
      // FQCN or prepends with "resolvers\" namespace.
      if ( strpos($options['class'], '\\') === false ) {
        $options['class'] = "resolvers\\$options[class]";
      }

      $args = array(
          new $options['class']((array) $options['options'])
        );

      if ( !empty($options['weight']) ) {
        $args[] = $options['weight'];
      }

      call_user_func_array(array($resolver, 'registerResolver'), $args);
    }
  });

  unset($resolvers);

Designated config format:

  [
    {
      "class": "MaintenanceResolver",
      "weight": 999,
      "options": { "$ref": "system::maintenance" }
    },
    {
      "class": "UserContextResolver",
      "weight": 100,
      "options": {
        "setup": { "$ref": "system::environment" } // This requires a comparison expression
      }
    },
    {
      "class": "AuthenticationResolver",
      "weight": 90,
      "options": {
        "statusCode": 404
      }
    },
    {
      "class": "LocaleResolver",
      "weight": 70,
      "options": {
        "default": { "$ref": "system::i18n.localeDefault", "$default": "en_US" } // This requires a default value
      }
    }
  ]