Dice\Dice::getRule PHP Method

getRule() public method

Returns the rule that will be applied to the class $name when calling create()
public getRule ( $name ) : array
return array The rules for the specified class
    public function getRule($name)
    {
        $lcName = strtolower(ltrim($name, '\\'));
        if (isset($this->rules[$lcName])) {
            return $this->rules[$lcName];
        }
        foreach ($this->rules as $key => $rule) {
            // Find a rule which matches the class described in $name where:
            if (empty($rule['instanceOf']) && $key !== '*' && is_subclass_of($name, $key) && (!array_key_exists('inherit', $rule) || $rule['inherit'] === true)) {
                // And that rule should be inherited to subclasses
                return $rule;
            }
        }
        // No rule has matched, return the default rule if it's set
        return isset($this->rules['*']) ? $this->rules['*'] : [];
    }

Usage Example

示例#1
0
文件: Xml.php 项目: samilesma/Dice
 private function loadV2(\SimpleXmlElement $xml, \Dice\Dice $dice)
 {
     foreach ($xml as $key => $value) {
         $rule = $dice->getRule((string) $value->name);
         if ($value->call) {
             foreach ($value->call as $name => $call) {
                 $callArgs = [];
                 foreach ($call->children() as $key => $param) {
                     $callArgs[] = $this->getComponent($param);
                 }
                 $rule['call'][] = [(string) $call['method'], $callArgs];
             }
         }
         if (isset($value['inherit'])) {
             $rule['inherit'] = $value['inherit'] == 'false' ? false : true;
         }
         if ($value['instanceOf']) {
             $rule['instanceOf'] = (string) $value['instanceOf'];
         }
         if (isset($value['shared'])) {
             $rule['shared'] = (string) $value['shared'] === 'true';
         }
         if ($value->constructParams) {
             foreach ($value->constructParams->children() as $child) {
                 $rule['constructParams'][] = $this->getComponent($child);
             }
         }
         if ($value->substitute) {
             foreach ($value->substitute as $use) {
                 $rule['substitutions'][(string) $use['as']] = $this->getComponent($use['use'], true);
             }
         }
         if ($value->shareInstances) {
             foreach ($value->shareInstances->children() as $share) {
                 $rule['shareInstances'][] = $this->getComponent($share);
             }
         }
         $dice->addRule((string) $value['name'], $rule);
     }
 }