Leafo\ScssPhp\Compiler::get PHP Method

get() public method

Get variable
public get ( string $name, boolean $shouldThrow = true, Leafo\ScssPhp\Compiler\Environment $env = null ) : mixed
$name string
$shouldThrow boolean
$env Leafo\ScssPhp\Compiler\Environment
return mixed
    public function get($name, $shouldThrow = true, Environment $env = null)
    {
        $normalizedName = $this->normalizeName($name);
        $specialContentKey = static::$namespaces['special'] . 'content';
        if (!isset($env)) {
            $env = $this->getStoreEnv();
        }
        $nextIsRoot = false;
        $hasNamespace = $normalizedName[0] === '^' || $normalizedName[0] === '@' || $normalizedName[0] === '%';
        for (;;) {
            if (array_key_exists($normalizedName, $env->store)) {
                return $env->store[$normalizedName];
            }
            if (!$hasNamespace && isset($env->marker)) {
                if (!$nextIsRoot && !empty($env->store[$specialContentKey])) {
                    $env = $env->store[$specialContentKey]->scope;
                    $nextIsRoot = true;
                    continue;
                }
                $env = $this->rootEnv;
                continue;
            }
            if (!isset($env->parent)) {
                break;
            }
            $env = $env->parent;
        }
        if ($shouldThrow) {
            $this->throwError("Undefined variable \${$name}");
        }
        // found nothing
    }

Usage Example

示例#1
0
 /**
  * Get variable
  *
  * @api
  *
  * @param string    $name
  * @param boolean   $shouldThrow
  * @param \stdClass $env
  *
  * @return mixed
  */
 public function get($name, $shouldThrow = true, $env = null)
 {
     try {
         return parent::get($name, $shouldThrow, $env);
     } catch (\Exception $e) {
         echo $e->getMessage() . "\n";
         return ['string', '', ['']];
     }
 }
Compiler