Zephir\Expression\Constants::compile PHP Method

compile() public method

Resolves a PHP constant value into C-code
public compile ( array $expression, Zephir\CompilationContext $compilationContext ) : Zephir\CompiledExpression
$expression array
$compilationContext Zephir\CompilationContext
return Zephir\CompiledExpression
    public function compile(array $expression, CompilationContext $compilationContext)
    {
        $isPhpConstant = false;
        $isZephirConstant = false;
        $constantName = $expression['value'];
        $mergedConstants = array_merge($this->envConstants, $this->magicConstants, $this->resources);
        if (!defined($expression['value']) && !in_array($constantName, $mergedConstants)) {
            if (!$compilationContext->compiler->isConstant($constantName)) {
                $compilationContext->logger->warning("Constant '" . $constantName . "' does not exist at compile time", 'nonexistent-constant', $expression);
            } else {
                $isZephirConstant = true;
            }
        } else {
            if (strpos($constantName, 'VERSION') !== false) {
                $isPhpConstant = false;
            } else {
                $isPhpConstant = true;
            }
        }
        if ($isZephirConstant && !in_array($constantName, $this->resources)) {
            $constant = $compilationContext->compiler->getConstant($constantName);
            return new LiteralCompiledExpression($constant[0], $constant[1], $expression);
        }
        if ($isPhpConstant && !in_array($constantName, $mergedConstants)) {
            $constantName = constant($constantName);
            $type = strtolower(gettype($constantName));
            switch ($type) {
                case 'integer':
                    return new LiteralCompiledExpression('int', $constantName, $expression);
                case 'double':
                    return new LiteralCompiledExpression('double', $constantName, $expression);
                case 'string':
                    return new LiteralCompiledExpression('string', Utils::addSlashes($constantName, true, Types::STRING), $expression);
                case 'object':
                    throw new CompilerException('?');
                default:
                    return new LiteralCompiledExpression($type, $constantName, $expression);
            }
        }
        if (in_array($constantName, $this->magicConstants)) {
            switch ($constantName) {
                case '__CLASS__':
                    return new CompiledExpression('string', Utils::addSlashes($compilationContext->classDefinition->getCompleteName(), true, Types::STRING), $expression);
                    //no break
                //no break
                case '__NAMESPACE__':
                    return new CompiledExpression('string', Utils::addSlashes($compilationContext->classDefinition->getNamespace(), true, Types::STRING), $expression);
                    //no break
                //no break
                case '__METHOD__':
                    return new CompiledExpression('string', $compilationContext->classDefinition->getName() . ':' . $compilationContext->currentMethod->getName(), $expression);
                    //no break
                //no break
                case '__FUNCTION__':
                    return new CompiledExpression('string', $compilationContext->currentMethod->getName(), $expression);
                    //no break
            }
            $compilationContext->logger->warning("Magic constant '" . $constantName . "' is not supported", 'not-supported-magic-constant', $expression);
            return new LiteralCompiledExpression('null', null, $expression);
        }
        if ($this->_expecting && $this->_expectingVariable) {
            $symbolVariable = $this->_expectingVariable;
            $symbolVariable->setLocalOnly(false);
            $symbolVariable->setMustInitNull(true);
            $symbolVariable->initVariant($compilationContext);
        } else {
            $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext, $expression);
        }
        if (!$symbolVariable->isVariable()) {
            throw new CompilerException('Cannot use variable: ' . $symbolVariable->getType() . ' to assign property value', $expression);
        }
        $compilationContext->codePrinter->output('ZEPHIR_GET_CONSTANT(' . $compilationContext->backend->getVariableCode($symbolVariable) . ', "' . $expression['value'] . '");');
        return new CompiledExpression('variable', $symbolVariable->getName(), $expression);
    }

Usage Example

Example #1
0
 /**
  * Process the value of the class constant if needed
  *
  * @param compilationContext $compilationContext
  */
 public function processValue($compilationContext)
 {
     if ($this->value['type'] == 'constant') {
         $constant = new Constants();
         $compiledExpression = $constant->compile($this->value, $compilationContext);
         $this->value = array('type' => $compiledExpression->getType(), 'value' => $compiledExpression->getCode());
         return;
     }
     if ($this->value['type'] == 'static-constant-access') {
         $expression = new Expression($this->value);
         $compiledExpression = $expression->compile($compilationContext);
         $this->value = array('type' => $compiledExpression->getType(), 'value' => $compiledExpression->getCode());
         return;
     }
 }
All Usage Examples Of Zephir\Expression\Constants::compile