code
stringlengths
31
1.39M
docstring
stringlengths
23
16.8k
func_name
stringlengths
1
126
language
stringclasses
1 value
repo
stringlengths
7
63
path
stringlengths
7
166
url
stringlengths
50
220
license
stringclasses
7 values
public function isVoid(): bool { return $this->void; }
Checks if the method must not return any value.
isVoid
php
zephir-lang/zephir
src/Class/Method/Method.php
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
MIT
public function preCompile(CompilationContext $compilationContext): void { $localContext = null; $typeInference = null; $callGathererPass = null; if (is_object($this->statements)) { $compilationContext->currentMethod = $this; /** * This pass checks for zval variables than can be potentially * used without allocating memory and track it * these variables are stored in the stack */ if ($compilationContext->config->get('local-context-pass', 'optimizations')) { $localContext = new LocalContextPass(); $localContext->pass($this->statements); } /** * This pass tries to infer types for dynamic variables * replacing them by low level variables */ if ($compilationContext->config->get('static-type-inference', 'optimizations')) { $typeInference = new StaticTypeInference(); $typeInference->pass($this->statements); if ($compilationContext->config->get('static-type-inference-second-pass', 'optimizations')) { $typeInference->reduce(); $typeInference->pass($this->statements); } } /** * This pass counts how many times a specific */ if ($compilationContext->config->get('call-gatherer-pass', 'optimizations')) { $callGathererPass = new CallGathererPass($compilationContext); $callGathererPass->pass($this->statements); } } $this->localContext = $localContext; $this->typeInference = $typeInference; $this->callGathererPass = $callGathererPass; }
Pre-compiles the method making compilation pass data (static inference, local-context-pass) available to other methods. @throws CompilerException
preCompile
php
zephir-lang/zephir
src/Class/Method/Method.php
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
MIT
public function setIsBundled(bool $bundled): void { $this->isBundled = $bundled; }
Sets if the method is bundled or not.
setIsBundled
php
zephir-lang/zephir
src/Class/Method/Method.php
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
MIT
public function setIsInitializer(bool $initializer): void { $this->isInitializer = $initializer; }
Sets if the method is an initializer or not.
setIsInitializer
php
zephir-lang/zephir
src/Class/Method/Method.php
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
MIT
public function setIsStatic(bool $static): void { $this->isStatic = $static; }
Sets if the method is internal or not.
setIsStatic
php
zephir-lang/zephir
src/Class/Method/Method.php
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
MIT
public function setReturnTypes(?array $returnType = null): void { $this->returnTypesRaw = $returnType; if (null === $returnType) { return; } if (isset($returnType['void']) && $returnType['void']) { $this->void = true; return; } if (!isset($returnType['list'])) { return; } $types = []; $castTypes = []; foreach ($returnType['list'] as $returnTypeItem) { /** * We continue the loop, because it only works for PHP >= 8.0. */ if (isset($returnTypeItem['data-type']) && $returnTypeItem['data-type'] === 'mixed') { $this->mixed = true; } if (!isset($returnTypeItem['cast'])) { $types[$returnTypeItem['data-type']] = $returnTypeItem; continue; } if (isset($returnTypeItem['cast']['collection'])) { continue; } if (isset($returnTypeItem['collection']) && $returnTypeItem['collection']) { $types['array'] = [ 'type' => 'return-type-parameter', 'data-type' => 'array', 'mandatory' => 0, 'file' => $returnTypeItem['cast']['file'], 'line' => $returnTypeItem['cast']['line'], 'char' => $returnTypeItem['cast']['char'], ]; } else { $castTypes[$returnTypeItem['cast']['value']] = $returnTypeItem['cast']['value']; } } if (count($castTypes) > 0) { $types['object'] = []; $this->returnClassTypes = $castTypes; } if (count($types) > 0) { $this->returnTypes = $types; } }
Process RAW return types structure. Example: ``` $returnType = [ 'type' => 'return-type', 'list' => [ [ 'type' => 'return-type-parameter', 'cast' => [ 'type' => 'variable', 'value' => '\StdClass', 'file' => './stubs.zep', 'line' => 21, 'char' => 48 ], 'collection' => 1, 'file' => './stubs.zep', 'line' => 21, 'char' => 48 ], [ 'type' => 'return-type-parameter', 'data-type' => 'bool', 'mandatory' => 0, 'file' => './stubs.zep', 'line' => 22, 'char' => 5 ] ], 'void' => 0, 'file' => './stubs.zep', 'line' => 22, 'char' => 5 ]; ``` @param array|null $returnType
setReturnTypes
php
zephir-lang/zephir
src/Class/Method/Method.php
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
MIT
public function setupOptimized(CompilationContext $compilationContext): self { if (!$compilationContext->config->get('internal-call-transformation', 'optimizations')) { return $this; } $classDefinition = $this->getClassDefinition(); /** * Skip for closures */ if ('__invoke' === $this->getName() || $classDefinition->isInterface()) { return $this; } if (!$this->isInternal() && !$classDefinition->isBundled()) { /* Not supported for now */ if ($this->getNumberOfRequiredParameters() != $this->getNumberOfParameters()) { return $this; } if ($this->isConstructor()) { return $this; } $optimizedName = $this->getName() . '_zephir_internal_call'; $visibility = ['internal']; $statements = null; if ($this->statements) { $statements = new StatementsBlock(json_decode(json_encode($this->statements->getStatements()), true)); } $optimizedMethod = new self( $classDefinition, $visibility, $optimizedName, $this->parameters, $statements, $this->docblock, null, $this->expression ); $optimizedMethod->typeInference = $this->typeInference; $optimizedMethod->setReturnTypes($this->returnTypes); $classDefinition->addMethod($optimizedMethod); } return $this; }
Generate internal method's based on the equivalent PHP methods, allowing bypassing php userspace for internal method calls.
setupOptimized
php
zephir-lang/zephir
src/Class/Method/Method.php
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
MIT
private function getParamDataType(array $parameter): string { return $parameter['data-type'] ?? 'variable'; }
Get data type of method's parameter
getParamDataType
php
zephir-lang/zephir
src/Class/Method/Method.php
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
MIT
private function processStaticConstantAccess( CompilationContext $compilationContext, array $parameter, Printer $oldCodePrinter, string $type ): string { /** * Now I can write code for easy use on Expression because * code in this method don't write with codePrinter ;(. * * TODO: Rewrite all to codePrinter */ $symbolVariable = $compilationContext->symbolTable->getVariableForWrite( $parameter['name'], $compilationContext, $parameter['default'] ); $expression = new Expression($parameter['default']); $expression->setExpectReturn(true, $symbolVariable); $compiledExpression = $expression->compile($compilationContext); if ($type !== $compiledExpression->getType()) { throw new CompilerException( 'Default parameter value type: ' . $compiledExpression->getType() . ' cannot be assigned to variable(' . $type . ')', $parameter ); } $parameter['default']['type'] = $compiledExpression->getType(); $parameter['default']['value'] = $compiledExpression->getCode(); $compilationContext->codePrinter = $oldCodePrinter; return $this->assignDefaultValue($parameter, $compilationContext); }
@param CompilationContext $compilationContext @param array $parameter @param Printer $oldCodePrinter @param string $type @return string @throws Exception @throws ReflectionException
processStaticConstantAccess
php
zephir-lang/zephir
src/Class/Method/Method.php
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
MIT
private function renderPhalconCompatible(): bool { $compatibilityClasses = require_once __DIR__ . '/../../config/phalcon-compatibility-headers.php'; $classDefinition = $this->functionLike->getClassDefinition(); $implementedInterfaces = $classDefinition !== null ? $classDefinition->getImplementedInterfaces() : []; $extendsClass = $classDefinition?->getExtendsClass(); if (empty($implementedInterfaces) && $extendsClass === null) { return false; } $methodName = $this->functionLike->getName(); if ($extendsClass !== null) { $implementedInterfaces = array_merge($implementedInterfaces, [$extendsClass]); } $found = false; foreach ($implementedInterfaces as $implementedInterface) { if (isset($compatibilityClasses[$implementedInterface][$methodName])) { foreach ($compatibilityClasses[$implementedInterface][$methodName] as $condition => $args) { $this->codePrinter->output($condition); foreach ($args as $arg) { $this->codePrinter->output( str_replace(['__ce__'], [$this->name], $arg) ); } } $this->codePrinter->output('#endif'); $found = true; } } return $found; }
Find from $compatibilityClasses and render specific hardcoded arg info for with specific PHP version conditions. This is temporary solution designed specifically for Phalcon project. @return bool @deprecated used as MVP solution for cross PHP versions support
renderPhalconCompatible
php
zephir-lang/zephir
src/Code/ArgInfoDefinition.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/ArgInfoDefinition.php
MIT
public function clear(): void { $this->code = ''; $this->lastLine = ''; $this->level = 0; }
Frees memory used within the code.
clear
php
zephir-lang/zephir
src/Code/Printer.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Printer.php
MIT
public function getNumberPrints(): int { return $this->currentPrints; }
Returns an approximate number of lines printed by the CodePrinter.
getNumberPrints
php
zephir-lang/zephir
src/Code/Printer.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Printer.php
MIT
public function getOutput(): string { return $this->code; }
Returns the output in the buffer.
getOutput
php
zephir-lang/zephir
src/Code/Printer.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Printer.php
MIT
public function outputBlankLine(bool $ifPrevNotBlank = false): void { if (!$ifPrevNotBlank) { $this->code .= PHP_EOL; $this->lastLine = PHP_EOL; ++$this->currentPrints; } else { if (trim($this->lastLine)) { $this->code .= PHP_EOL; $this->lastLine = PHP_EOL; ++$this->currentPrints; } } }
Adds a blank line to the output Optionally controlling if the blank link must be added if the previous line added isn't one blank line too.
outputBlankLine
php
zephir-lang/zephir
src/Code/Printer.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Printer.php
MIT
public function outputDocBlock($docblock, bool $replaceTab = true): void { $code = ''; $docblock = '/' . $docblock . '/'; foreach (explode("\n", $docblock) as $line) { if ($replaceTab) { $code .= str_repeat("\t", $this->level) . preg_replace('/^[ \t]+/', ' ', $line) . PHP_EOL; } else { $code .= $line . PHP_EOL; } } $this->lastLine = $code; $this->code .= $code; ++$this->currentPrints; }
Adds a comment to the output with indentation level.
outputDocBlock
php
zephir-lang/zephir
src/Code/Printer.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Printer.php
MIT
public function outputNoIndent(string $code): void { $this->lastLine = $code; $this->code .= $code . PHP_EOL; ++$this->currentPrints; }
Add code to the output without indentation.
outputNoIndent
php
zephir-lang/zephir
src/Code/Printer.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Printer.php
MIT
public function preOutput(string $code): void { $this->lastLine = $code; $this->code = str_repeat("\t", $this->level) . $code . PHP_EOL . $this->code; ++$this->currentPrints; }
Add code to the output at the beginning.
preOutput
php
zephir-lang/zephir
src/Code/Printer.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Printer.php
MIT
public function preOutputBlankLine(bool $ifPrevNotBlank = false): void { if (!$ifPrevNotBlank) { $this->code = PHP_EOL . $this->code; $this->lastLine = PHP_EOL; ++$this->currentPrints; } else { if (trim($this->lastLine)) { $this->code = PHP_EOL . $this->code; $this->lastLine = PHP_EOL; ++$this->currentPrints; } } }
Adds a blank line to the output Optionally controlling if the blank link must be added if the previous line added isn't one blank line too.
preOutputBlankLine
php
zephir-lang/zephir
src/Code/Printer.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Printer.php
MIT
public function getCDefault(string $name, array $global, string $namespace): string { if (!isset($global['default'])) { throw new RuntimeException('Field "' . $name . '" does not have a default value'); } return match ($global['type']) { Types::T_BOOL, Types::T_BOOLEAN => '', Types::T_STRING => "\t" . $namespace . '_globals->' . $this->simpleName . '.' . $name . ' = ZSTR_VAL(zend_string_init(ZEND_STRL("' . $global['default'] . '"), 0));', Types::T_INT, Types::T_UINT, Types::T_LONG, Types::T_DOUBLE, Types::T_HASH => "\t" . $namespace . '_globals->' . $this->simpleName . '.' . $name . ' = ' . $global['default'] . ';', default => throw new InvalidArgumentException( 'Unknown global type: ' . $global['type'] ), }; }
Returns the C code that initializes the extension global. @throws RuntimeException @throws InvalidArgumentException
getCDefault
php
zephir-lang/zephir
src/Code/Builder/Struct.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Builder/Struct.php
MIT
public function getInitEntry(string $name, array $global, string $namespace): string { $structName = $this->simpleName . '.' . $name; $iniEntry = $global['ini-entry'] ?? []; $iniName = $iniEntry['name'] ?? $namespace . '.' . $structName; $scope = $iniEntry['scope'] ?? 'PHP_INI_ALL'; return match ($global['type']) { Types::T_BOOLEAN, Types::T_BOOL => 'STD_PHP_INI_BOOLEAN("' . $iniName . '", "' . (int)(true === $global['default']) . '", ' . $scope . ', OnUpdateBool, ' . $structName . ', zend_' . $namespace . '_globals, ' . $namespace . '_globals)', Types::T_STRING => sprintf( 'STD_PHP_INI_ENTRY(%s, %s, %s, NULL, %s, %s, %s)', '"' . $iniName . '"', '"' . $global['default'] . '"', $scope, $structName, 'zend_' . $namespace . '_globals', $namespace . '_globals', ), default => '', }; }
Process Globals for phpinfo() page. @see https://docs.zephir-lang.com/latest/en/globals @param string $name - global-name @param array $global - global structure (type, default...) @param string $namespace - global namespace @return string
getInitEntry
php
zephir-lang/zephir
src/Code/Builder/Struct.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Builder/Struct.php
MIT
protected function convertToCType(string $type): string { return match ($type) { 'boolean', 'bool' => 'zend_bool', 'hash' => 'HashTable* ', 'string' => 'zend_string* ', 'int', 'uint', 'long', 'char', 'uchar', 'double' => $type, default => throw new InvalidArgumentException( 'Unknown global type: ' . $type ), }; }
Generates the internal c-type according to the php's type. @throws InvalidArgumentException
convertToCType
php
zephir-lang/zephir
src/Code/Builder/Struct.php
https://github.com/zephir-lang/zephir/blob/master/src/Code/Builder/Struct.php
MIT
public function create(string $className, string $filePath): FileInterface { $compiler = new CompilerFile($this->config, new AliasManager(), $this->filesystem); $compiler->setClassName($className); $compiler->setFilePath($filePath); $compiler->setLogger($this->logger); return $compiler; }
Creates CompilerFile instance. NOTE: Each instance of CompilerFile must have its own AliasManager instance.
create
php
zephir-lang/zephir
src/Compiler/CompilerFileFactory.php
https://github.com/zephir-lang/zephir/blob/master/src/Compiler/CompilerFileFactory.php
MIT
public function __construct() { $this->setDetectionFlags(self::DETECT_NONE); }
ForValueUseDetector constructor. Initialize detector with safe defaults
__construct
php
zephir-lang/zephir
src/Detectors/ForValueUseDetector.php
https://github.com/zephir-lang/zephir/blob/master/src/Detectors/ForValueUseDetector.php
MIT
public function detect(string $variable, array $statements): bool { $this->passStatementBlock($statements); return $this->getNumberOfMutations($variable) > 0; }
Do the detection pass on a single variable.
detect
php
zephir-lang/zephir
src/Detectors/WriteDetector.php
https://github.com/zephir-lang/zephir/blob/master/src/Detectors/WriteDetector.php
MIT
public function getNumberOfMutations(string $variable): int { return $this->mutations[$variable] ?? 0; }
Returns the number of assignment instructions that mutated a variable.
getNumberOfMutations
php
zephir-lang/zephir
src/Detectors/WriteDetector.php
https://github.com/zephir-lang/zephir/blob/master/src/Detectors/WriteDetector.php
MIT
public function increaseMutations(string $variable): self { if (isset($this->mutations[$variable])) { ++$this->mutations[$variable]; } else { $this->mutations[$variable] = 1; } return $this; }
Increase the number of mutations a variable has inside a statement block.
increaseMutations
php
zephir-lang/zephir
src/Detectors/WriteDetector.php
https://github.com/zephir-lang/zephir/blob/master/src/Detectors/WriteDetector.php
MIT
public function getAnnotationsByType(string $type): array { $annotation = []; foreach ($this->annotations as $an) { if ($an->getName() === $type) { $annotation[] = $an; } } return $annotation; }
@param string $type the annotation name you want to get @return Annotation[] an array containing the annotations matching the name
getAnnotationsByType
php
zephir-lang/zephir
src/Documentation/Docblock.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Docblock.php
MIT
public function getParsedDocblock() { return $this->docblockObj; }
return the parsed docblock. It will return null until you run the parse() method. @return Docblock the parsed docblock
getParsedDocblock
php
zephir-lang/zephir
src/Documentation/DocblockParser.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/DocblockParser.php
MIT
public function parse() { $this->docblockObj = new Docblock(); $this->ignoreSpaces = false; $this->ignoreStar = true; $this->commentOpen = false; $this->annotationNameOpen = false; $this->annotationOpen = false; $this->summaryOpen = true; $this->descriptionOpen = false; $this->currentAnnotationStr = null; $this->currentAnnotationContentStr = null; $this->summaryStr = ''; $this->descriptionStr = ''; $this->currentLine = 0; $this->currentCharIndex = 0; $this->annotationLen = strlen($this->annotation); $this->currentChar = $this->annotation[0]; while (null !== $this->currentChar) { $currentChar = $this->currentChar; if ($this->ignoreSpaces && ctype_space($currentChar)) { } else { if (!$this->commentOpen) { if ('/' == $currentChar) { if ('*' == $this->nextCharacter() && '*' == $this->nextCharacter()) { $this->commentOpen = true; } else { continue; } } } else { if ('*' == $currentChar) { $nextCharacter = $this->nextCharacter(); if ('/' == $nextCharacter) { // stop annotation parsing on end of comment $this->__tryRegisterAnnotation(); break; } elseif ($this->ignoreStar) { if (' ' == $nextCharacter) { $this->nextCharacter(); } continue; } } if ('@' == $currentChar) { $this->descriptionStr = trim($this->descriptionStr); if ($this->descriptionOpen && $this->descriptionStr !== '') { $this->descriptionOpen = false; } $this->currentAnnotationStr = ''; $this->currentAnnotationContentStr = ''; $this->ignoreSpaces = false; $this->annotationNameOpen = true; } elseif ($this->annotationNameOpen || $this->annotationOpen) { // stop annotation parsing on new line if ("\n" == $currentChar || "\r" == $currentChar) { $this->__tryRegisterAnnotation(); $this->ignoreSpaces = false; $this->ignoreStar = true; } elseif ($this->annotationNameOpen) { if (ctype_space($currentChar)) { $this->annotationNameOpen = false; $this->annotationOpen = true; } else { $this->currentAnnotationStr .= $currentChar; } } elseif ($this->annotationOpen) { $this->currentAnnotationContentStr .= $currentChar; } } elseif ($this->summaryOpen) { // stop summary on new line if ($this->summaryStr !== '' && ("\n" == $currentChar || "\r" == $currentChar)) { $this->summaryOpen = false; $this->descriptionOpen = true; $this->ignoreStar = true; } else { $this->summaryStr .= $currentChar; } } elseif ($this->descriptionOpen) { // stop description on new line if ("\n" == $currentChar || "\r" == $currentChar) { $this->descriptionStr .= PHP_EOL; } else { $this->descriptionStr .= $currentChar; } } } } $this->nextCharacter(); } $this->docblockObj->setSummary(trim($this->summaryStr)); $this->docblockObj->setDescription(trim($this->descriptionStr)); return $this->docblockObj; }
Parses the internal annotation string. @return Docblock the parsed docblock
parse
php
zephir-lang/zephir
src/Documentation/DocblockParser.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/DocblockParser.php
MIT
private function __createAnnotation(string $name, string $string) { switch ($name) { case 'link': $annotation = new Annotation\Link($name, $string); $annotation->getLinkText(); break; case 'return': $annotation = new Annotation\ReturnAnnotation($name, $string); $annotation->getReturnType(); break; case 'see': $annotation = new Annotation\See($name, $string); $annotation->getResource(); break; default: $annotation = new Annotation($name, $string); break; } return $annotation; }
@param string $name the annotation name @param string $string the annotation name @return Annotation
__createAnnotation
php
zephir-lang/zephir
src/Documentation/DocblockParser.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/DocblockParser.php
MIT
private function __tryRegisterAnnotation(): void { if (($this->annotationNameOpen || $this->annotationOpen) && $this->currentAnnotationStr !== '') { $annotation = $this->__createAnnotation($this->currentAnnotationStr, $this->currentAnnotationContentStr); $this->docblockObj->addAnnotation($annotation); } $this->annotationNameOpen = false; $this->annotationOpen = false; }
check if there is a currently parsed annotation, registers it, and stops the current annotation parsing.
__tryRegisterAnnotation
php
zephir-lang/zephir
src/Documentation/DocblockParser.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/DocblockParser.php
MIT
private function nextCharacter() { ++$this->currentCharIndex; if ($this->annotationLen <= $this->currentCharIndex) { $this->currentChar = null; } else { $this->currentChar = $this->annotation[$this->currentCharIndex]; } return $this->currentChar; }
moves the current cursor to the next character. @return string the new current character
nextCharacter
php
zephir-lang/zephir
src/Documentation/DocblockParser.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/DocblockParser.php
MIT
public function setPathToRoot(string $pathToRoot): void { $this->pathToRoot = $pathToRoot; }
the path to root for the hyperlink in the templates.
setPathToRoot
php
zephir-lang/zephir
src/Documentation/Template.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Template.php
MIT
public function setProjectConfig($projectConfig): void { $this->projectConfig = $projectConfig; }
set the config of the project (it usually wraps the version, the theme config, etc...). @param array $projectConfig
setProjectConfig
php
zephir-lang/zephir
src/Documentation/Template.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Template.php
MIT
public function setThemeOptions(array $themeOptions): void { $this->themeOptions = $themeOptions; }
add theme options to make them available during the render phase.
setThemeOptions
php
zephir-lang/zephir
src/Documentation/Template.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Template.php
MIT
public function buildStaticDirectory(): void { $outputStt = $this->getOutputPath('asset'); if (!file_exists($outputStt)) { mkdir($outputStt, 0777, true); } $this->extendedTheme?->buildStaticDirectory(); $themeStt = $this->getThemePath('static'); if ($themeStt) { $files = []; $this->__copyDir($themeStt, $outputStt . '/static', $files); foreach ($files as $f) { foreach ($this->options as $optName => $opt) { $fcontent = file_get_contents($f); $fcontent = str_replace('%_' . $optName . '_%', $opt, $fcontent); file_put_contents($f, $fcontent); } } } }
copy the static directory of the theme into the output directory.
buildStaticDirectory
php
zephir-lang/zephir
src/Documentation/Theme.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Theme.php
MIT
public function drawFile(FileInterface $file): void { $outputFile = ltrim($file->getOutputFile(), '/'); $output = pathinfo($this->outputDir . '/' . $outputFile); $outputDirname = $output['dirname']; $outputBasename = $output['basename']; $outputFilename = $outputDirname . '/' . $outputBasename; // todo : check if writable if (!file_exists($outputDirname)) { mkdir($outputDirname, 0777, true); } $subDirNumber = count(explode('/', $outputFile)) - 1; if ($subDirNumber > 0) { $pathToRoot = str_repeat('../', $subDirNumber); } else { $pathToRoot = './'; } $template = new Template($this, $file->getData(), $file->getTemplateName()); $template->setPathToRoot($pathToRoot); $template->setThemeOptions($this->options); $template->setProjectConfig($this->projectConfig); touch($outputFilename); $template->write($outputFilename); }
Parse and draw the specified file. @param FileInterface $file @throws Exception
drawFile
php
zephir-lang/zephir
src/Documentation/Theme.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Theme.php
MIT
public function getThemeInfo($name) { if (isset($this->themeInfos[$name])) { return $this->themeInfos[$name]; } return null; }
Get assets from the theme info (theme.json file placed inside the theme directory). @param string $name @return mixed|null
getThemeInfo
php
zephir-lang/zephir
src/Documentation/Theme.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Theme.php
MIT
public function getThemeInfoExtendAware($name) { if ($this->extendedTheme) { $data = $this->extendedTheme->getThemeInfoExtendAware($name); } else { $data = []; } $info = $this->getThemeInfo($name); array_unshift($data, $info); return $data; }
Similar with getThemeInfo but includes the value for all extended themes, and returns the results as an array. @param string $name @return array
getThemeInfoExtendAware
php
zephir-lang/zephir
src/Documentation/Theme.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Theme.php
MIT
public function getThemePath($path) { $path = pathinfo($this->themeDir . '/' . $path); $pathDirname = $path['dirname']; $pathBasename = $path['basename']; $pathFilename = $pathDirname . '/' . $pathBasename; if (!file_exists($pathFilename)) { return null; } return $pathFilename; }
find the path to a file in the theme. @param $path @return string
getThemePath
php
zephir-lang/zephir
src/Documentation/Theme.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Theme.php
MIT
public function getThemePathExtendsAware($path) { $newPath = $this->getThemePath($path); if (!$newPath) { if ($this->extendedTheme) { return $this->extendedTheme->getThemePathExtendsAware($path); } } return $newPath; }
find the path to a file in the theme or from the extended theme. @param $path @return string
getThemePathExtendsAware
php
zephir-lang/zephir
src/Documentation/Theme.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Theme.php
MIT
private function __copyDir($src, $dst, &$files = null): void { $dir = opendir($src); @mkdir($dst); while (false !== ($file = readdir($dir))) { if (('.' != $file) && ('..' != $file)) { if (is_dir($src . '/' . $file)) { $this->__copyDir($src . '/' . $file, $dst . '/' . $file, $files); } else { copy($src . '/' . $file, $dst . '/' . $file); if (is_array($files)) { $files[] = $dst . '/' . $file; } } } } closedir($dir); }
from : https://stackoverflow.com/questions/2050859/copy-entire-contents-of-a-directory-to-another-using-php. @param $src @param $dst @param $files
__copyDir
php
zephir-lang/zephir
src/Documentation/Theme.php
https://github.com/zephir-lang/zephir/blob/master/src/Documentation/Theme.php
MIT
public function __construct( string $message = '', array $extra = [], int $code = 0, ?Throwable $previous = null, ) { if (isset($extra['file'])) { $message .= ' in ' . $extra['file'] . ' on line ' . $extra['line']; } $this->extra = $extra; parent::__construct($message, $code, $previous); }
CompilerException constructor. @param string $message the Exception message to throw [optional] @param array $extra extra info [optional] @param int $code the Exception code [optional] @param Throwable|null $previous the previous throwable used for the exception chaining [optional]
__construct
php
zephir-lang/zephir
src/Exception/CompilerException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/CompilerException.php
MIT
public static function cannotUseAsArray( string $type, array $extra = [], int $code = 0, ?Throwable $previous = null, ): self { return new self( "Cannot use variable type: '" . $type . "' as array", $extra, $code, $previous ); }
Cannot use variable type as array @param string $type the variable type @param array $extra extra info [optional] @param int $code the Exception code [optional] @param Throwable|null $previous the previous throwable used for the exception chaining [optional] @return self
cannotUseAsArray
php
zephir-lang/zephir
src/Exception/CompilerException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/CompilerException.php
MIT
public static function cannotUseNonInitializedVariableAsObject( array $extra = [], int $code = 0, ?Throwable $previous = null, ): self { return new self( 'Cannot use non-initialized variable as an object', $extra, $code, $previous ); }
Cannot use non-initialized variable as an object @param array $extra extra info [optional] @param int $code the Exception code [optional] @param Throwable|null $previous the previous throwable used for the exception chaining [optional] @return self
cannotUseNonInitializedVariableAsObject
php
zephir-lang/zephir
src/Exception/CompilerException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/CompilerException.php
MIT
public static function cannotUseValueTypeAs( Variable $variable, string $asType, ?array $extra = null, int $code = 0, ?Throwable $previous = null, ): self { return new self( 'Cannot use value type: ' . $variable->getType() . ' as ' . $asType, $extra, $code, $previous ); }
Cannot use non-initialized variable as an object @param array|null $extra extra info [optional] @param int $code the Exception code [optional] @param Exception|Throwable|null $previous the previous throwable used for the exception chaining [optional] @return self
cannotUseValueTypeAs
php
zephir-lang/zephir
src/Exception/CompilerException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/CompilerException.php
MIT
public static function cannotUseVariableTypeAs( Variable $variable, string $asType, ?array $extra = null, int $code = 0, ?Throwable $previous = null, ): self { return new self( 'Cannot use variable type: ' . $variable->getType() . ' ' . $asType, $extra, $code, $previous ); }
Cannot use non-initialized variable as an object @param array|null $extra extra info [optional] @param int $code the Exception code [optional] @param Throwable|null $previous the previous throwable used for the exception chaining [optional] @return self
cannotUseVariableTypeAs
php
zephir-lang/zephir
src/Exception/CompilerException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/CompilerException.php
MIT
public static function classDoesNotHaveProperty( string $className, string $property, ?array $extra = null, int $code = 0, ?Throwable $previous = null, ): self { return new self( "Class '" . $className . "' does not have a property called: '" . $property . "'", $extra, $code, $previous ); }
Class does not have a property called @param string $className @param string $property @param array|null $extra @param int $code @param Throwable|null $previous @return self
classDoesNotHaveProperty
php
zephir-lang/zephir
src/Exception/CompilerException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/CompilerException.php
MIT
public static function illegalOperationTypeOnStaticVariable( string $operator, string $dataType, array $statement, ): self { return new self( "Operator '$operator' isn't supported for static variables and $dataType typed expressions", $statement ); }
@param string $operator @param string $dataType @param array $statement @return self
illegalOperationTypeOnStaticVariable
php
zephir-lang/zephir
src/Exception/CompilerException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/CompilerException.php
MIT
public static function returnValuesVariantVars( ?array $extra = null, int $code = 0, ?Throwable $previous = null, ): self { return new self( 'Returned values by functions can only be assigned to variant variables', $extra, $code, $previous ); }
Returned Values can only be assigned to variant variables @param array|null $extra extra info [optional] @param int $code the Exception code [optional] @param Throwable|null $previous the previous throwable used for the exception chaining [optional] @return self
returnValuesVariantVars
php
zephir-lang/zephir
src/Exception/CompilerException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/CompilerException.php
MIT
public static function unknownType( Variable $variable, ?array $extra = null, int $code = 0, ?Throwable $previous = null, ): self { return new self( "Unknown '" . $variable->getType() . "'", $extra, $code, $previous ); }
Unknown variable type @param array|null $extra @param int $code @param Throwable|null $previous @return self
unknownType
php
zephir-lang/zephir
src/Exception/CompilerException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/CompilerException.php
MIT
public static function unsupportedType( CompiledExpression $expression, ?array $extra = null, int $code = 0, ?Throwable $previous = null, ): self { return new self( 'Unsupported type: ' . $expression->getType(), $extra, $code, $previous ); }
Unsupported Type @param CompiledExpression $expression @param array|null $extra @param int $code @param Throwable|null $previous @return self
unsupportedType
php
zephir-lang/zephir
src/Exception/CompilerException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/CompilerException.php
MIT
public function __construct(array $statement, TypeAwareInterface $type, array $extra = null) { $message = sprintf( "Operator '%s' is not supported for variable type: %s", $statement['operator'], $type->getType() ); parent::__construct($message, $extra ?: $statement); }
IllegalOperationException constructor. @param array $statement The statement @param TypeAwareInterface $type Operator type @param array|null $extra extra info [optional]
__construct
php
zephir-lang/zephir
src/Exception/IllegalOperationException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/IllegalOperationException.php
MIT
public function __construct(string $type, array $expression = null) { $message = sprintf( 'Returning type: %s but this type is not compatible with return-type hints declared in the method', $type ); parent::__construct($message, $expression); }
InvalidTypeException constructor. @param string $type @param array|null $expression
__construct
php
zephir-lang/zephir
src/Exception/InvalidTypeException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/InvalidTypeException.php
MIT
public function __construct( string $message = '', ?array $extra = null, int $code = 0, Exception | Throwable $previous = null ) { if (is_array($extra) && isset($extra['file'])) { $message .= ' in ' . $extra['file'] . ' on line ' . $extra['line']; } $this->extra = $extra; parent::__construct($message, $code, $previous); }
ParseException constructor. @param string $message the Exception message to throw [optional] @param array|null $extra extra info [optional] @param int $code the Exception code [optional] @param Exception|Throwable|null $previous the previous throwable used for the exception chaining [optional]
__construct
php
zephir-lang/zephir
src/Exception/ParseException.php
https://github.com/zephir-lang/zephir/blob/master/src/Exception/ParseException.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $classDefinition = new Definition( $compilationContext->config->get('namespace'), self::$id . '__closure' ); $classDefinition->setIsFinal(true); $compilerFile = new CompilerFileAnonymous($classDefinition, $compilationContext->config, $compilationContext); $compilerFile->setLogger($compilationContext->logger); $compilationContext->compiler->addClassDefinition($compilerFile, $classDefinition); $parameters = null; if (isset($expression['left'])) { $parameters = new Parameters($expression['left']); } $block = $expression['right'] ?? []; $staticVariables = []; if (isset($expression['use']) && is_array($expression['use'])) { foreach ($expression['use'] as $parameter) { $staticVariables[$parameter['name']] = $compilationContext->symbolTable->getVariable( $parameter['name'] ); } } foreach ($staticVariables as $var) { $classDefinition->addProperty( new Property( $classDefinition, ['public', 'static'], $var->getName(), null, null, null ) ); } $classMethod = new Method( $classDefinition, ['public', 'final'], '__invoke', $parameters, new StatementsBlock($block), null, null, $expression, $staticVariables ); $symbolVariable = $this->generateClosure( $classDefinition, $classMethod, $block, $compilationContext, $expression ); $compilationContext->headersManager->add('kernel/object'); foreach ($staticVariables as $var) { if (in_array($var->getType(), ['variable', 'array'])) { $compilationContext->backend->updateStaticProperty( $classDefinition->getClassEntry(), $var->getName(), $var, $compilationContext ); continue; } $tempVariable = $compilationContext->symbolTable->getTempNonTrackedVariable( 'variable', $compilationContext, true ); switch ($var->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'char': case 'uchar': $compilationContext->backend->assignLong($tempVariable, $var, $compilationContext); break; case 'double': $compilationContext->backend->assignDouble($tempVariable, $var, $compilationContext); break; case 'bool': $compilationContext->backend->assignBool($tempVariable, $var, $compilationContext); break; case 'string': $compilationContext->backend->assignString($tempVariable, $var, $compilationContext); break; default: break; } $compilationContext->backend->updateStaticProperty( $classDefinition->getClassEntry(), $var->getName(), $tempVariable, $compilationContext ); } ++self::$id; return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
Creates a closure. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception
compile
php
zephir-lang/zephir
src/Expression/Closure.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Closure.php
MIT
public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; }
Sets if the variable must be resolved into a direct variable symbol create a temporary value or ignore the return value.
setExpectReturn
php
zephir-lang/zephir
src/Expression/Closure.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Closure.php
MIT
public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; }
Sets if the result of the evaluated expression is read only. @param bool $readOnly
setReadOnly
php
zephir-lang/zephir
src/Expression/Closure.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Closure.php
MIT
protected function generateClosure( Definition $classDefinition, Method $classMethod, mixed $block, CompilationContext $compilationContext, array $expression ): ?Variable { $classDefinition->addMethod($classMethod, $block); $compilationContext->headersManager->add('kernel/object'); if ($this->expecting) { if ($this->expectingVariable) { $symbolVariable = $this->expectingVariable; } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); } $symbolVariable->initVariant($compilationContext); $compilationContext->backend->createClosure($symbolVariable, $classDefinition, $compilationContext); return $symbolVariable; }
@param Definition $classDefinition @param Method $classMethod @param mixed $block @param CompilationContext $compilationContext @param array $expression @return Variable|null
generateClosure
php
zephir-lang/zephir
src/Expression/Closure.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Closure.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $classDefinition = new Definition( $compilationContext->config->get('namespace'), self::$id . '__closure' ); $classDefinition->setIsFinal(true); $compilerFile = new CompilerFileAnonymous($classDefinition, $compilationContext->config); $compilerFile->setLogger($compilationContext->logger); $compilationContext->compiler->addClassDefinition($compilerFile, $classDefinition); /** * Builds parameters using the only parameter available. */ $parameters = new Parameters([ [ 'type' => 'parameter', 'name' => $expression['left']['value'], 'const' => 0, 'data-type' => 'variable', 'mandatory' => 0, 'reference' => 0, 'file' => $expression['left']['file'], 'line' => $expression['left']['line'], 'char' => $expression['left']['char'], ], ]); $exprBuilder = BuilderFactory::getInstance(); $statementBlockBuilder = $exprBuilder->statements()->block([ $exprBuilder->statements() ->returnX($exprBuilder->raw($expression['right'])), ]); $block = $statementBlockBuilder->build(); $classMethod = new Method( $classDefinition, ['public', 'final'], '__invoke', $parameters, new StatementsBlock($block), null, null, $expression ); $symbolVariable = $this->generateClosure( $classDefinition, $classMethod, $block, $compilationContext, $expression ); ++self::$id; return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
Creates a closure. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws CompilerException
compile
php
zephir-lang/zephir
src/Expression/ClosureArrow.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/ClosureArrow.php
MIT
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 { $isPhpConstant = !str_contains($constantName, 'VERSION'); } 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', Name::addSlashes($constantName), $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', Name::addSlashes($compilationContext->classDefinition->getCompleteName()), $expression ); case '__NAMESPACE__': return new CompiledExpression( 'string', Name::addSlashes($compilationContext->classDefinition->getNamespace()), $expression ); case '__METHOD__': return new CompiledExpression( 'string', $compilationContext->classDefinition->getName( ) . ':' . $compilationContext->currentMethod->getName(), $expression ); case '__FUNCTION__': return new CompiledExpression( 'string', $compilationContext->currentMethod->getName(), $expression ); } $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); }
Resolves a PHP constant value into C-code. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws CompilerException
compile
php
zephir-lang/zephir
src/Expression/Constants.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Constants.php
MIT
public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; }
Sets if the variable must be resolved into a direct variable symbol create a temporary value or ignore the return value.
setExpectReturn
php
zephir-lang/zephir
src/Expression/Constants.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Constants.php
MIT
public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; }
Sets if the result of the evaluated expression is read only.
setReadOnly
php
zephir-lang/zephir
src/Expression/Constants.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Constants.php
MIT
public function compile(array $expression, CompilationContext $compilationContext) { /** * Resolves the symbol that expects the value */ if ($this->expecting) { if ($this->expectingVariable) { $symbolVariable = $this->expectingVariable; if ('variable' != $symbolVariable->getType() && 'array' != $symbolVariable->getType()) { throw CompilerException::cannotUseVariableTypeAs( $symbolVariable, 'as an array', $expression ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'array', $compilationContext, $expression ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'array', $compilationContext, $expression ); } if (!isset($expression['left'])) { if ($this->expectingVariable) { $symbolVariable->initVariant($compilationContext); } /** * Mark the variable as an array */ $symbolVariable->setDynamicTypes('array'); return new CompiledExpression('array', $symbolVariable->getRealName(), $expression); } $codePrinter = $compilationContext->codePrinter; $compilationContext->headersManager->add('kernel/array'); /** * This calculates a prime number bigger than the current array size to possibly * reduce hash collisions when adding new members to the array. */ $arrayLength = count($expression['left']); if ($arrayLength >= 33 && function_exists('gmp_nextprime')) { $arrayLength = (int)gmp_strval(gmp_nextprime($arrayLength - 1)); } if ($this->expectingVariable && $symbolVariable->getVariantInits() >= 1) { $symbolVariable = $compilationContext->symbolTable->addTemp('variable', $compilationContext); $symbolVariable->initVariant($compilationContext); $compilationContext->backend->initArray( $symbolVariable, $compilationContext, $arrayLength > 0 ? $arrayLength : null ); $symbolVariable->setDynamicTypes('array'); } else { if ($this->expectingVariable) { $symbolVariable->initVariant($compilationContext); } /** * Mark the variable as an array */ $symbolVariable->setDynamicTypes('array'); $compilationContext->backend->initArray( $symbolVariable, $compilationContext, $arrayLength > 0 ? $arrayLength : null ); } foreach ($expression['left'] as $item) { if (!isset($item['key'])) { $expr = new Expression($item['value']); $resolvedExpr = $expr->compile($compilationContext); $itemVariable = $this->getArrayValue($resolvedExpr, $compilationContext); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); $item = $compilationContext->backend->resolveValue($itemVariable, $compilationContext); $codePrinter->output('zephir_array_fast_append(' . $symbol . ', ' . $item . ');'); $this->checkVariableTemporal($itemVariable); continue; } $exprKey = new Expression($item['key']); $resolvedExprKey = $exprKey->compile($compilationContext); switch ($resolvedExprKey->getType()) { case Types::T_STRING: $expr = new Expression($item['value']); $resolvedExpr = $expr->compile($compilationContext); switch ($resolvedExpr->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: case Types::T_DOUBLE: case Types::T_STRING: $compilationContext->backend->addArrayEntry( $symbolVariable, $resolvedExprKey, $resolvedExpr, $compilationContext ); break; case Types::T_BOOL: $compilationContext->headersManager->add('kernel/array'); if ('true' == $resolvedExpr->getCode()) { $compilationContext->backend->updateArray( $symbolVariable, $resolvedExprKey, 'true', $compilationContext, 'PH_COPY | PH_SEPARATE' ); } else { $compilationContext->backend->updateArray( $symbolVariable, $resolvedExprKey, 'false', $compilationContext, 'PH_COPY | PH_SEPARATE' ); } break; case Types::T_NULL: $compilationContext->backend->updateArray( $symbolVariable, $resolvedExprKey, 'null', $compilationContext, 'PH_COPY | PH_SEPARATE' ); break; case Types::T_ARRAY: case Types::T_VARIABLE: $valueVariable = $this->getArrayValue($resolvedExpr, $compilationContext); $compilationContext->backend->updateArray( $symbolVariable, $resolvedExprKey, $valueVariable, $compilationContext, 'PH_COPY | PH_SEPARATE' ); $this->checkVariableTemporal($valueVariable); break; default: throw new CompilerException( 'Invalid value type: ' . $resolvedExpr->getType(), $item['value'] ); } break; case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: $expr = new Expression($item['value']); $resolvedExpr = $expr->compile($compilationContext); switch ($resolvedExpr->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: case Types::T_DOUBLE: case Types::T_STRING: $compilationContext->backend->addArrayEntry( $symbolVariable, $resolvedExprKey, $resolvedExpr, $compilationContext ); break; case Types::T_BOOL: if ('true' == $resolvedExpr->getCode()) { $compilationContext->backend->updateArray( $symbolVariable, $resolvedExprKey, 'true', $compilationContext, 'PH_COPY' ); } else { $compilationContext->backend->updateArray( $symbolVariable, $resolvedExprKey, 'false', $compilationContext, 'PH_COPY' ); } break; case Types::T_NULL: $compilationContext->backend->updateArray( $symbolVariable, $resolvedExprKey, 'null', $compilationContext, 'PH_COPY' ); break; case Types::T_ARRAY: case Types::T_VARIABLE: $valueVariable = $this->getArrayValue($resolvedExpr, $compilationContext); $compilationContext->backend->updateArray( $symbolVariable, $resolvedExprKey, $valueVariable, $compilationContext, 'PH_COPY' ); $this->checkVariableTemporal($valueVariable); break; default: throw new CompilerException( 'Invalid value type: ' . $item['value']['type'], $item['value'] ); } break; case Types::T_VARIABLE: $variableVariable = $compilationContext->symbolTable->getVariableForRead( $resolvedExprKey->getCode(), $compilationContext, $item['key'] ); switch ($variableVariable->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: $expr = new Expression($item['value']); $resolvedExpr = $expr->compile($compilationContext); switch ($resolvedExpr->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: case Types::T_BOOL: case Types::T_DOUBLE: case Types::T_NULL: case Types::T_STRING: $compilationContext->backend->addArrayEntry( $symbolVariable, $resolvedExprKey, $resolvedExpr, $compilationContext ); break; case Types::T_VARIABLE: $valueVariable = $this->getArrayValue($resolvedExpr, $compilationContext); $compilationContext->backend->updateArray( $symbolVariable, $resolvedExprKey, $valueVariable, $compilationContext, 'PH_COPY' ); $this->checkVariableTemporal($valueVariable); break; default: throw new CompilerException( 'Invalid value type: ' . $item['value']['type'], $item['value'] ); } break; case Types::T_STRING: $expr = new Expression($item['value']); $resolvedExpr = $expr->compile($compilationContext); switch ($resolvedExpr->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: $codePrinter->output( 'add_assoc_long_ex(' . $symbolVariable->getName( ) . ', Z_STRVAL_P(' . $resolvedExprKey->getCode( ) . '), Z_STRLEN_P(' . $item['key']['value'] . '), ' . $resolvedExpr->getCode( ) . ');' ); break; case Types::T_DOUBLE: $codePrinter->output( 'add_assoc_double_ex(' . $symbolVariable->getName( ) . ', Z_STRVAL_P(' . $resolvedExprKey->getCode( ) . '), Z_STRLEN_P(' . $item['key']['value'] . '), ' . $resolvedExpr->getCode( ) . ');' ); break; case Types::T_BOOL: $codePrinter->output( 'add_assoc_bool_ex(' . $symbolVariable->getName( ) . ', Z_STRVAL_P(' . $resolvedExprKey->getCode( ) . '), Z_STRLEN_P(' . $item['key']['value'] . '), ' . $resolvedExpr->getBooleanCode( ) . ');' ); break; case Types::T_STRING: $compilationContext->backend->addArrayEntry( $symbolVariable, $resolvedExprKey, $resolvedExpr, $compilationContext ); break; case Types::T_NULL: $codePrinter->output( 'add_assoc_null_ex(' . $symbolVariable->getName( ) . ', Z_STRVAL_P(' . $resolvedExprKey->getCode( ) . '), Z_STRLEN_P(' . $item['key']['value'] . ') + 1);' ); break; case Types::T_VARIABLE: $valueVariable = $this->getArrayValue($resolvedExpr, $compilationContext); $compilationContext->backend->updateArray( $symbolVariable, $resolvedExprKey, $resolvedExpr, $compilationContext ); $this->checkVariableTemporal($valueVariable); break; default: throw new CompilerException( 'Invalid value type: ' . $resolvedExpr->getType(), $item['value'] ); } break; case Types::T_VARIABLE: $expr = new Expression($item['value']); $resolvedExpr = $expr->compile($compilationContext); switch ($resolvedExpr->getType()) { case Types::T_NULL: case Types::T_VARIABLE: case Types::T_BOOL: $valueVariable = $this->getArrayValue($resolvedExpr, $compilationContext); $compilationContext->backend->updateArray( $symbolVariable, $variableVariable, $valueVariable, $compilationContext ); $this->checkVariableTemporal($valueVariable); break; default: throw new CompilerException( 'Invalid value type: ' . $item['value']['type'], $item['value'] ); } break; default: throw CompilerException::cannotUseVariableTypeAs( $variableVariable, 'as an array index', $expression ); } break; default: throw new CompilerException( 'Invalid key type: ' . $resolvedExprKey->getType(), $item['key'] ); } } return new CompiledExpression('array', $symbolVariable->getRealName(), $expression); }
Compiles an array initialization. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws ReflectionException @throws Exception
compile
php
zephir-lang/zephir
src/Expression/NativeArray.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/NativeArray.php
MIT
public function getArrayValue( CompiledExpression $exprCompiled, CompilationContext $compilationContext ) { switch ($exprCompiled->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); $compilationContext->backend->assignLong($tempVar, $exprCompiled->getCode(), $compilationContext); return $tempVar; case Types::T_CHAR: case Types::T_UCHAR: $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); $compilationContext->backend->assignLong( $tempVar, '\'' . $exprCompiled->getCode() . '\'', $compilationContext ); return $tempVar; case Types::T_DOUBLE: $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); $compilationContext->backend->assignDouble($tempVar, $exprCompiled->getCode(), $compilationContext); return $tempVar; case Types::T_BOOL: if ('true' === $exprCompiled->getCode()) { return new GlobalConstant('ZEPHIR_GLOBAL(global_true)'); } if ('false' === $exprCompiled->getCode()) { return new GlobalConstant('ZEPHIR_GLOBAL(global_false)'); } $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); $compilationContext->backend->assignBool($tempVar, $exprCompiled->getCode(), $compilationContext); return $tempVar; case Types::T_NULL: return new GlobalConstant('ZEPHIR_GLOBAL(global_null)'); case Types::T_STRING: case Types::T_ULONG: $tempVar = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext); $compilationContext->backend->assignString($tempVar, $exprCompiled->getCode(), $compilationContext); return $tempVar; case Types::T_ARRAY: return $compilationContext->symbolTable->getVariableForRead( $exprCompiled->getCode(), $compilationContext, $exprCompiled->getOriginal() ); case Types::T_VARIABLE: $itemVariable = $compilationContext->symbolTable->getVariableForRead( $exprCompiled->getCode(), $compilationContext, $exprCompiled->getOriginal() ); switch ($itemVariable->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: $tempVar = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext ); $compilationContext->backend->assignLong($tempVar, $itemVariable, $compilationContext); return $tempVar; case Types::T_DOUBLE: $tempVar = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext ); $compilationContext->backend->assignDouble($tempVar, $itemVariable, $compilationContext); return $tempVar; case Types::T_BOOL: $tempVar = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext ); $compilationContext->backend->assignBool($tempVar, $itemVariable, $compilationContext); return $tempVar; case Types::T_STRING: case Types::T_VARIABLE: case Types::T_ARRAY: case Types::T_MIXED: return $itemVariable; default: throw new CompilerException('Unknown ' . $itemVariable->getType(), $itemVariable); } break; default: throw new CompilerException('Unknown', $exprCompiled); } }
Resolves an item to be added in an array. @param CompiledExpression $exprCompiled @param CompilationContext $compilationContext @return GlobalConstant|Variable
getArrayValue
php
zephir-lang/zephir
src/Expression/NativeArray.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/NativeArray.php
MIT
public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; }
Sets if the variable must be resolved into a direct variable symbol create a temporary value or ignore the return value. @param bool $expecting @param Variable|null $expectingVariable
setExpectReturn
php
zephir-lang/zephir
src/Expression/NativeArray.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/NativeArray.php
MIT
public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; }
Sets if the result of the evaluated expression is read only. @param bool $readOnly
setReadOnly
php
zephir-lang/zephir
src/Expression/NativeArray.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/NativeArray.php
MIT
public function compile($expression, CompilationContext $compilationContext) { /** * Resolve the left part of the expression. */ $expr = new Expression($expression['left']); $expr->setReadOnly(true); $exprVariable = $expr->compile($compilationContext); /** * Only dynamic variables can be used as arrays */ switch ($exprVariable->getType()) { case 'variable': $variableVariable = $compilationContext->symbolTable->getVariableForRead( $exprVariable->getCode(), $compilationContext, $expression ); switch ($variableVariable->getType()) { case 'variable': case 'array': case 'string': break; default: throw new CompilerException( 'Variable type: ' . $variableVariable->getType() . ' cannot be used as array', $expression['left'] ); } break; default: throw new CompilerException( 'Cannot use expression: ' . $exprVariable->getType() . ' as an array', $expression['left'] ); } /** * Resolve the dimension according to variable's type */ switch ($variableVariable->getType()) { case 'array': case 'variable': return $this->accessDimensionArray($expression, $variableVariable, $compilationContext); case 'string': return $this->accessStringOffset($expression, $variableVariable, $compilationContext); } }
Compiles foo[x] = {expr}. @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Expression/NativeArrayAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/NativeArrayAccess.php
MIT
public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; }
Sets if the variable must be resolved into a direct variable symbol create a temporary value or ignore the return value.
setExpectReturn
php
zephir-lang/zephir
src/Expression/NativeArrayAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/NativeArrayAccess.php
MIT
public function setNoisy(bool $noisy): void { $this->noisy = $noisy; }
Sets whether the expression must be resolved in "noisy" mode.
setNoisy
php
zephir-lang/zephir
src/Expression/NativeArrayAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/NativeArrayAccess.php
MIT
public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; }
Sets if the result of the evaluated expression is read only.
setReadOnly
php
zephir-lang/zephir
src/Expression/NativeArrayAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/NativeArrayAccess.php
MIT
protected function accessDimensionArray( array $expression, Variable $variableVariable, CompilationContext $compilationContext ): CompiledExpression { $arrayAccess = $expression; if ('variable' == $variableVariable->getType()) { if ($variableVariable->hasAnyDynamicType('unknown')) { throw new CompilerException('Cannot use non-initialized variable as an array', $arrayAccess['left']); } /** * Trying to use a non-object dynamic variable as object */ if ($variableVariable->hasDifferentDynamicType(['undefined', 'array', 'null'])) { $compilationContext->logger->warning( 'Possible attempt to access array-index on a non-array dynamic variable', ['non-array-access', $arrayAccess['left']] ); } } /** * Resolves the symbol that expects the value. */ $readOnly = false; $symbolVariable = $this->expectingVariable; if ($this->readOnly) { if ($this->expecting && $this->expectingVariable) { /** * If a variable is assigned once in the method, we try to promote it * to a read only variable */ if ('return_value' != $symbolVariable->getName()) { $line = $compilationContext->symbolTable->getLastCallLine(); if (false === $line || ($line > 0 && $line < $expression['line'])) { $numberMutations = $compilationContext->symbolTable->getExpectedMutations( $symbolVariable->getName() ); if (1 == $numberMutations) { if ($symbolVariable->getNumberMutations() == $numberMutations) { $symbolVariable->setMemoryTracked(false); $readOnly = true; } } } } /** * Variable is not read-only or it wasn't promoted */ if (!$readOnly) { if ('return_value' != $symbolVariable->getName()) { $symbolVariable->observeVariant($compilationContext); $this->readOnly = false; } else { $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedUninitializedVariable( 'variable', $compilationContext, ); } } } else { $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedUninitializedVariable( 'variable', $compilationContext, ); } } else { if ($this->expecting && $this->expectingVariable) { /** * If a variable is assigned once in the method, we try to promote it * to a read only variable */ if ('return_value' !== $symbolVariable->getName()) { $line = $compilationContext->symbolTable->getLastCallLine(); if (false === $line || ($line > 0 && $line < $expression['line'])) { $numberMutations = $compilationContext->symbolTable->getExpectedMutations( $symbolVariable->getName() ); if (1 == $numberMutations) { if ($symbolVariable->getNumberMutations() == $numberMutations) { $symbolVariable->setMemoryTracked(false); $readOnly = true; } } } } /** * Variable is not read-only or it wasn't promoted */ if (!$readOnly) { if ('return_value' != $symbolVariable->getName()) { $symbolVariable->observeVariant($compilationContext); $this->readOnly = false; } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve( 'variable', $compilationContext ); } } } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve( 'variable', $compilationContext ); } } /** * Variable that receives property accesses must be polymorphic */ if (!$symbolVariable->isVariable()) { throw new CompilerException( 'Cannot use variable: ' . $symbolVariable->getType() . ' to assign array index', $expression ); } /** * At this point, we don't know the type fetched from the index */ $symbolVariable->setDynamicTypes('undefined'); if ($this->readOnly || $readOnly) { $flags = $this->noisy ? 'PH_NOISY | PH_READONLY' : 'PH_READONLY'; } else { $flags = $this->noisy ? 'PH_NOISY' : '0'; } /** * Right part of expression is the index. */ $expr = new Expression($arrayAccess['right']); $exprIndex = $expr->compile($compilationContext); $compilationContext->headersManager->add('kernel/array'); if ('variable' === $exprIndex->getType()) { $exprIndex = $compilationContext->symbolTable->getVariableForRead( $exprIndex->getCode(), $compilationContext, $expression ); } $compilationContext->backend->arrayFetch( $symbolVariable, $variableVariable, $exprIndex, $flags, $arrayAccess, $compilationContext ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Variable $variableVariable @param CompilationContext $compilationContext @return CompiledExpression @throws Exception @throws ReflectionException
accessDimensionArray
php
zephir-lang/zephir
src/Expression/NativeArrayAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/NativeArrayAccess.php
MIT
protected function accessStringOffset( array $expression, Variable $variableVariable, CompilationContext $compilationContext ): CompiledExpression { if ($this->expecting) { if ($this->expectingVariable) { $symbolVariable = $this->expectingVariable; if ('char' != $symbolVariable->getType() && 'uchar' != $symbolVariable->getType()) { $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedVariable( 'uchar', $compilationContext ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedVariable( 'uchar', $compilationContext ); } } /** * Right part of expression is the index. */ $expr = new Expression($expression['right']); $exprIndex = $expr->compile($compilationContext); $codePrinter = $compilationContext->codePrinter; $variableCode = $compilationContext->backend->getVariableCode($variableVariable); switch ($exprIndex->getType()) { case 'int': case 'uint': case 'long': $compilationContext->headersManager->add('kernel/operators'); $codePrinter->output( $symbolVariable->getName( ) . ' = ZEPHIR_STRING_OFFSET(' . $variableCode . ', ' . $exprIndex->getCode() . ');' ); break; case 'variable': $variableIndex = $compilationContext->symbolTable->getVariableForRead( $exprIndex->getCode(), $compilationContext, $expression ); switch ($variableIndex->getType()) { case 'int': case 'uint': case 'long': $codePrinter->output( $symbolVariable->getName( ) . ' = ZEPHIR_STRING_OFFSET(' . $variableCode . ', ' . $variableIndex->getName() . ');' ); break; default: throw new CompilerException( 'Cannot use index type ' . $variableIndex->getType() . ' as offset', $expression['right'] ); } break; default: throw new CompilerException( 'Cannot use index type ' . $exprIndex->getType() . ' as offset', $expression['right'] ); } return new CompiledExpression('variable', $symbolVariable->getName(), $expression); }
@param array $expression @param Variable $variableVariable @param CompilationContext $compilationContext @return CompiledExpression @throws ReflectionException @throws Exception
accessStringOffset
php
zephir-lang/zephir
src/Expression/NativeArrayAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/NativeArrayAccess.php
MIT
public function compile($expression, CompilationContext $compilationContext): CompiledExpression { $propertyAccess = $expression; $expr = new Expression($propertyAccess['left']); $expr->setReadOnly(true); $exprVariable = $expr->compile($compilationContext); switch ($exprVariable->getType()) { case 'variable': $variableVariable = $compilationContext->symbolTable->getVariableForRead( $exprVariable->getCode(), $compilationContext, $expression ); switch ($variableVariable->getType()) { case 'variable': break; default: throw new CompilerException( 'Variable type: ' . $variableVariable->getType() . ' cannot be used as object', $propertyAccess['left'] ); } break; default: throw new CompilerException( 'Cannot use expression: ' . $exprVariable->getType() . ' as an object', $propertyAccess['left'] ); } $property = $propertyAccess['right']['value']; $propertyDefinition = null; $classDefinition = null; $currentClassDefinition = $compilationContext->classDefinition; /** * If the property is accessed on 'this', we check if the method does exist */ if ('this' == $variableVariable->getRealName()) { $classDefinition = $currentClassDefinition; $this->checkClassHasProperty( $classDefinition, $property, $expression ); $propertyDefinition = $classDefinition->getProperty($property); } else { /** * If we know the class related to a variable we could check if the property * is defined on that class */ if ($variableVariable->hasAnyDynamicType('object')) { $classType = current($variableVariable->getClassTypes()); $compiler = $compilationContext->compiler; if ($classType !== false && $compiler->isClass($classType)) { $classDefinition = $compiler->getClassDefinition($classType); if (!$classDefinition) { throw new CompilerException( 'Cannot locate class definition for class: ' . $classType, $expression ); } $this->checkClassHasProperty( $classDefinition, $property, $expression, $classType ); $propertyDefinition = $classDefinition->getProperty($property); } } } /** * Having a proper propertyDefinition we can check if the property is readable * according to its modifiers */ if ($propertyDefinition) { if ($propertyDefinition->isStatic()) { throw new CompilerException( "Attempt to access static property '" . $property . "' as non static", $expression ); } if (!$propertyDefinition->isPublic()) { /** * Protected variables only can be read in the class context * where they were declared */ if ($classDefinition == $currentClassDefinition) { if ($propertyDefinition->isPrivate()) { $declarationDefinition = $propertyDefinition->getClassDefinition(); if ($declarationDefinition !== $currentClassDefinition) { throw new CompilerException( "Attempt to access private property '" . $property . "' outside of its declared class context: '" . $declarationDefinition->getCompleteName() . "'", $expression ); } } } else { if (!$propertyDefinition->isProtected() && $propertyDefinition->isPrivate()) { $declarationDefinition = $propertyDefinition->getClassDefinition(); if ($declarationDefinition !== $currentClassDefinition) { throw new CompilerException( "Attempt to access private property '" . $property . "' outside of its declared class context: '" . $declarationDefinition->getCompleteName() . "'", $expression ); } } } } } /** * Resolves the symbol that expects the value. */ $readOnly = false; $makeSymbolVariable = false; if ($this->expecting) { if ($this->expectingVariable) { $symbolVariable = $this->expectingVariable; /** * If a variable is assigned once in the method, we try to promote it * to a read only variable */ if ('return_value' != $symbolVariable->getName()) { $line = $compilationContext->symbolTable->getLastCallLine(); if (false === $line || ($line > 0 && $line < $expression['line'])) { $numberMutations = $compilationContext->symbolTable->getExpectedMutations( $symbolVariable->getName() ); if (1 == $numberMutations) { if ($symbolVariable->getNumberMutations() == $numberMutations) { $symbolVariable->setMemoryTracked(false); $readOnly = true; } } } } /** * Variable is not read only, or it wasn't promoted */ if (!$readOnly) { if ('return_value' != $symbolVariable->getName()) { $symbolVariable->observeVariant($compilationContext); } else { $makeSymbolVariable = true; } } $this->readOnly = false; } else { $makeSymbolVariable = true; } } else { $makeSymbolVariable = true; } $readOnly = $this->readOnly || $readOnly; if ($makeSymbolVariable) { if ($readOnly) { $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedVariable( 'variable', $compilationContext ); } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve( 'variable', $compilationContext ); } } /** * Variable that receives a property value must be polymorphic */ if (!$symbolVariable->isVariable()) { throw new CompilerException( 'Cannot use variable: ' . $symbolVariable->getType() . ' to assign property value', $expression ); } /** * At this point, we don't know the exact dynamic type fetched from the property */ $symbolVariable->setDynamicTypes('undefined'); $compilationContext->headersManager->add('kernel/object'); $compilationContext->backend->fetchProperty( $symbolVariable, $variableVariable, $property, $readOnly, $compilationContext ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
Resolves the access to a property in an object. @throws ReflectionException @throws Exception
compile
php
zephir-lang/zephir
src/Expression/PropertyAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/PropertyAccess.php
MIT
public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; }
Sets if the variable must be resolved into a direct variable symbol create a temporary value or ignore the return value.
setExpectReturn
php
zephir-lang/zephir
src/Expression/PropertyAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/PropertyAccess.php
MIT
public function setNoisy(bool $noisy): void { $this->noisy = $noisy; }
Sets whether the expression must be resolved in "noisy" mode.
setNoisy
php
zephir-lang/zephir
src/Expression/PropertyAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/PropertyAccess.php
MIT
public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; }
Sets if the result of the evaluated expression is read only.
setReadOnly
php
zephir-lang/zephir
src/Expression/PropertyAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/PropertyAccess.php
MIT
public function compile($expression, CompilationContext $compilationContext) { $propertyAccess = $expression; $expr = new Expression($propertyAccess['left']); $exprVariable = $expr->compile($compilationContext); switch ($exprVariable->getType()) { case 'variable': $variableVariable = $compilationContext->symbolTable->getVariableForRead( $exprVariable->getCode(), $compilationContext, $expression ); switch ($variableVariable->getType()) { case 'variable': break; default: throw new CompilerException( 'Variable type: ' . $variableVariable->getType() . ' cannot be used as object', $propertyAccess['left'] ); } break; default: throw new CompilerException( 'Cannot use expression: ' . $exprVariable->getType() . ' as an object', $propertyAccess['left'] ); } $propertyVariable = match ($propertyAccess['right']['type']) { Types::T_VARIABLE => $compilationContext->symbolTable->getVariableForRead( $propertyAccess['right']['value'], $compilationContext, $expression ), Types::T_STRING => null, default => throw new CompilerException( 'Variable type: ' . $propertyAccess['right']['type'] . ' cannot be used as object', $propertyAccess['left'] ), }; /** * Resolves the symbol that expects the value */ if ($this->expecting) { if ($this->expectingVariable) { $symbolVariable = $this->expectingVariable; if ('return_value' != $symbolVariable->getName()) { $symbolVariable->observeVariant($compilationContext); } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve( 'variable', $compilationContext ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserve( 'variable', $compilationContext ); } } /** * Variable that receives a property value must be polymorphic */ if ($symbolVariable && !$symbolVariable->isVariable()) { throw new CompilerException( 'Cannot use variable: ' . $symbolVariable->getType() . ' to assign property value', $expression ); } /* * At this point, we don't know the exact dynamic type fetched from the property */ $symbolVariable->setDynamicTypes('undefined'); $compilationContext->headersManager->add('kernel/object'); $property = $propertyVariable ?: Name::addSlashes($expression['right']['value']); $compilationContext->backend->fetchProperty( $symbolVariable, $variableVariable, $property, false, $compilationContext ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
Resolves the access to a property in an object. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression
compile
php
zephir-lang/zephir
src/Expression/PropertyDynamicAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/PropertyDynamicAccess.php
MIT
public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; }
Sets if the variable must be resolved into a direct variable symbol create a temporary value or ignore the return value.
setExpectReturn
php
zephir-lang/zephir
src/Expression/PropertyDynamicAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/PropertyDynamicAccess.php
MIT
public function setNoisy(bool $noisy): void { $this->noisy = $noisy; }
Sets whether the expression must be resolved in "noisy" mode.
setNoisy
php
zephir-lang/zephir
src/Expression/PropertyDynamicAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/PropertyDynamicAccess.php
MIT
public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; }
Sets if the result of the evaluated expression is read only.
setReadOnly
php
zephir-lang/zephir
src/Expression/PropertyDynamicAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/PropertyDynamicAccess.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { /** * Resolves the symbol that expects the value */ if ($this->expecting) { if ($this->expectingVariable) { $symbolVariable = $this->expectingVariable; if ('variable' !== $symbolVariable->getType()) { throw CompilerException::cannotUseVariableTypeAs( $symbolVariable, 'to store a reference', $expression ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); } $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly($this->readOnly); $left = $leftExpr->compile($compilationContext); if (!in_array($left->getType(), $this->validTypes)) { throw new CompilerException('Cannot obtain a reference from type: ' . $left->getType(), $expression); } $leftVariable = $compilationContext->symbolTable->getVariableForRead( $left->getCode(), $compilationContext, $expression ); if (!in_array($leftVariable->getType(), $this->validTypes)) { throw new CompilerException( 'Cannot obtain reference from variable type: ' . $leftVariable->getType(), $expression ); } $symbolVariable->setMustInitNull(true); $compilationContext->symbolTable->mustGrownStack(true); $symbolVariable->increaseVariantIfNull(); $compilationContext->codePrinter->output( 'ZEPHIR_MAKE_REFERENCE(' . $symbolVariable->getName() . ', ' . $leftVariable->getName() . ');' ); return new CompiledExpression('reference', $symbolVariable->getRealName(), $expression); }
Compiles a reference to a value. @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Expression/Reference.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Reference.php
MIT
public function setExpectReturn(bool $expecting, Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; }
Sets if the variable must be resolved into a direct variable symbol create a temporary value or ignore the return value. @param bool $expecting @param Variable|null $expectingVariable
setExpectReturn
php
zephir-lang/zephir
src/Expression/Reference.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Reference.php
MIT
public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; }
Sets if the result of the evaluated expression is read only. @param bool $readOnly
setReadOnly
php
zephir-lang/zephir
src/Expression/Reference.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Reference.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $compiler = &$compilationContext->compiler; $className = $expression['left']['value']; $constant = $expression['right']['value']; /** * Fetch the class definition according to the class where the constant * is supposed to be declared */ if (!in_array($className, ['this', 'self', 'static', 'parent'])) { $className = $compilationContext->getFullName($className); if ($compiler->isClass($className) || $compiler->isInterface($className)) { $classDefinition = $compiler->getClassDefinition($className); } else { if ($compiler->isBundledClass($className) || $compiler->isBundledInterface($className)) { $classDefinition = $compiler->getInternalClassDefinition($className); } else { throw new CompilerException("Cannot locate class '" . $className . "'", $expression['left']); } } } else { if (in_array($className, ['self', 'static', 'this'])) { $classDefinition = $compilationContext->classDefinition; } elseif ('parent' === $className) { $classDefinition = $compilationContext->classDefinition; $extendsClass = $classDefinition->getExtendsClass(); if (!$extendsClass) { throw new CompilerException( sprintf( 'Cannot find constant called "%s" on parent because class %s does not extend any class', $constant, $classDefinition->getCompleteName() ), $expression ); } else { $classDefinition = $classDefinition->getExtendsClassDefinition(); } } } /** * Constants are resolved to their values at compile time, * so we need to check that they effectively do exist */ if (!$classDefinition->hasConstant($constant)) { throw new CompilerException( sprintf( "Class '%s' does not have a constant called: '%s'", $classDefinition->getCompleteName(), $constant ), $expression ); } /** * We can optimize the reading of constants by avoiding query their value every time */ if (!$compilationContext->config->get('static-constant-class-folding', 'optimizations')) { /** * Resolves the symbol that expects the value */ if ($this->expecting) { if ($this->expectingVariable) { $symbolVariable = $this->expectingVariable; $symbolVariable->initVariant($compilationContext); } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); } /** * Variable that receives property accesses must be polymorphic */ if (!$symbolVariable->isVariable()) { throw new CompilerException( 'Cannot use variable: ' . $symbolVariable->getType() . ' to assign class constants', $expression ); } $symbolVariable->setDynamicTypes('undefined'); $compilationContext->headersManager->add('kernel/object'); $compilationContext->codePrinter->output( sprintf( 'zephir_get_class_constant(%s, %s, SS("%s"));', $symbolVariable->getName(), $classDefinition->getClassEntry($compilationContext), $constant ) ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); } $constantDefinition = $classDefinition->getConstant($constant); if ($constantDefinition instanceof Constant) { $constantDefinition->processValue($compilationContext); $value = $constantDefinition->getValueValue(); $type = $constantDefinition->getValueType(); } else { $value = $constantDefinition; $type = gettype($value); if ('integer' === $type) { $type = 'int'; } } switch ($type) { case 'string': case 'int': case 'double': case 'float': case 'bool': case 'null': break; default: $compilationContext->logger->warning( "Constant '" . $constantDefinition->getName() . "' does not exist at compile time", ['nonexistent-constant', $expression] ); return new CompiledExpression('null', null, $expression); } return new CompiledExpression($type, $value, $expression); }
Access a static constant class. @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Expression/StaticConstantAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/StaticConstantAccess.php
MIT
public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; }
Sets if the variable must be resolved into a direct variable symbol create a temporary value or ignore the return value.
setExpectReturn
php
zephir-lang/zephir
src/Expression/StaticConstantAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/StaticConstantAccess.php
MIT
public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; }
Sets if the result of the evaluated expression is read only.
setReadOnly
php
zephir-lang/zephir
src/Expression/StaticConstantAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/StaticConstantAccess.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $className = $expression['left']['value']; $compiler = $compilationContext->compiler; $property = $expression['right']['value']; /** * Fetch the class definition according to the class where the constant * is supposed to be declared */ if (!in_array($className, ['self', 'static', 'parent'])) { $className = $compilationContext->getFullName($className); if ($compiler->isClass($className)) { $classDefinition = $compiler->getClassDefinition($className); } else { if ($compiler->isBundledClass($className)) { $classDefinition = $compiler->getInternalClassDefinition($className); } else { throw new CompilerException("Cannot locate class '" . $className . "'", $expression['left']); } } } else { if (in_array($className, ['self', 'static'])) { $classDefinition = $compilationContext->classDefinition; } else { if ('parent' === $className) { $classDefinition = $compilationContext->classDefinition; if (!$classDefinition->getExtendsClass()) { throw new CompilerException( 'Cannot access static property "' . $property . '" on parent because class ' . $classDefinition->getCompleteName() . ' does not extend any class', $expression ); } else { $classDefinition = $classDefinition->getExtendsClassDefinition(); } } } } $this->checkClassHasProperty( $classDefinition, $property, $expression ); /** @var Property $propertyDefinition */ $propertyDefinition = $classDefinition->getProperty($property); $this->checkAccessNonStaticProperty( $propertyDefinition, $classDefinition, $property, $expression ); if ($propertyDefinition->isPrivate()) { if ($classDefinition != $compilationContext->classDefinition) { throw new CompilerException( "Cannot access private static property '" . $classDefinition->getCompleteName() . '::' . $property . "' out of its declaring context", $expression ); } } if ( $propertyDefinition->isProtected() && $classDefinition != $compilationContext->classDefinition && $classDefinition != $compilationContext->classDefinition->getExtendsClassDefinition() ) { throw new CompilerException( "Cannot access protected static property '" . $classDefinition->getCompleteName() . '::' . $property . "' out of its declaring context", $expression ); } /** * Resolves the symbol that expects the value */ if ($this->expecting) { if ($this->expectingVariable) { $symbolVariable = $this->expectingVariable; if ('return_value' === $symbolVariable->getName()) { $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedVariable( 'variable', $compilationContext ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedVariable( 'variable', $compilationContext ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempNonTrackedVariable( 'variable', $compilationContext ); } /** * Variable that receives property accesses must be polymorphic */ if (!$symbolVariable->isVariable()) { throw new CompilerException( 'Cannot use variable: ' . $symbolVariable->getType() . ' to assign class constants', $expression ); } $symbolVariable->setDynamicTypes('undefined'); $compilationContext->headersManager->add('kernel/object'); if (!$this->readOnly && 'return_value' != $symbolVariable->getName()) { $symbolVariable->observeVariant($compilationContext); } $compilationContext->backend->fetchStaticProperty( $symbolVariable, $classDefinition, $property, $this->readOnly, $compilationContext ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
Access a static property. @throws ReflectionException @throws Exception
compile
php
zephir-lang/zephir
src/Expression/StaticPropertyAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/StaticPropertyAccess.php
MIT
public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; }
Sets if the variable must be resolved into a direct variable symbol create a temporary value or ignore the return value.
setExpectReturn
php
zephir-lang/zephir
src/Expression/StaticPropertyAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/StaticPropertyAccess.php
MIT
public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; }
Sets if the result of the evaluated expression is read only.
setReadOnly
php
zephir-lang/zephir
src/Expression/StaticPropertyAccess.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/StaticPropertyAccess.php
MIT
public function get() { return $this->build(); }
@return array @deprecated since version 0.8.0, to be removed in 1.0. Use {@link \Zephir\Builder\AbstractBuilder::build()}
get
php
zephir-lang/zephir
src/Expression/Builder/AbstractBuilder.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Builder/AbstractBuilder.php
MIT
public function setParseInfo($file, $line, $char = null) { $this->setFile($file); $this->setLine($line); if (null !== $char) { $this->setChar($char); } return $this; }
@param string $file @param int $line @param null $char @return $this
setParseInfo
php
zephir-lang/zephir
src/Expression/Builder/AbstractBuilder.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Builder/AbstractBuilder.php
MIT
public function literal($type, $value = null) { return $this->raw([ 'type' => $type, 'value' => $value, ]); }
@param $type @param null $value @return RawExpression
literal
php
zephir-lang/zephir
src/Expression/Builder/BuilderFactory.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Builder/BuilderFactory.php
MIT
public function __construct($operator = null, AbstractBuilder $expression = null) { if (null !== $operator) { $this->setOperator($operator); } if (null !== $expression) { $this->setExpression($expression); } }
@param null $operator @param AbstractBuilder|null $expression
__construct
php
zephir-lang/zephir
src/Expression/Builder/Operators/UnaryOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Builder/Operators/UnaryOperator.php
MIT
public function __construct( $variable = null, $name = null, array $parameters = null, $type = self::TYPE_CALL_DIRECT ) { parent::__construct($name, $parameters, $type); if (null !== $name) { $this->setVariable($variable); } }
@param null $variable @param null $name @param array|null $parameters @param int $type
__construct
php
zephir-lang/zephir
src/Expression/Builder/Statements/CallMethodStatement.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Builder/Statements/CallMethodStatement.php
MIT
public function __construct($class = null, $method = null, array $parameters = null) { if (null !== $class) { $this->setClass($class); } if (null !== $method) { $this->setMethod($method); } if (null !== $parameters) { $this->setArguments($parameters); } }
@param null $class @param null $method @param array|null $parameters
__construct
php
zephir-lang/zephir
src/Expression/Builder/Statements/CallStaticStatement.php
https://github.com/zephir-lang/zephir/blob/master/src/Expression/Builder/Statements/CallStaticStatement.php
MIT
public function getHashFile(string $algorithm, string $sourceFile, bool $useCache = false): string { if (false === $useCache) { return hash_file($algorithm, $sourceFile); } $cacheFile = sprintf( '%s/%s/%s.%s', $this->basePath, $this->localPath, $this->normalizePath($sourceFile), $algorithm ); if (!is_file($cacheFile)) { $contents = hash_file($algorithm, $sourceFile); file_put_contents($cacheFile, $contents); return $contents; } if (filemtime($sourceFile) > filemtime($cacheFile)) { $contents = hash_file($algorithm, $sourceFile); file_put_contents($cacheFile, $contents); return $contents; } return file_get_contents($cacheFile); }
This function does not perform operations in the temporary directory, but it caches the results to avoid reprocessing. @param string $algorithm @param string $sourceFile @param bool $useCache @return string
getHashFile
php
zephir-lang/zephir
src/FileSystem/HardDisk.php
https://github.com/zephir-lang/zephir/blob/master/src/FileSystem/HardDisk.php
MIT
public static function persistByHash(string $data, string $path): int | bool { if (!file_exists($path)) { return file_put_contents($path, $data); } if (md5($data) !== md5_file($path)) { return file_put_contents($path, $data); } return false; }
Checks if the content of the file on the disk is the same as the content.
persistByHash
php
zephir-lang/zephir
src/FileSystem/HardDisk.php
https://github.com/zephir-lang/zephir/blob/master/src/FileSystem/HardDisk.php
MIT
private function cleanExtraPlaceholders(string $output): string { if (str_contains($output, '%')) { $output = preg_replace('/%(?:extra|context)\..+?%/', '', $output); $output = preg_replace('/ %type%\n/', "\n", $output); $output = preg_replace('/on line %line%/', '', $output); $output = preg_replace('/ in %file% /', '', $output); } return $output; }
Remove leftover %extra.xxx% and %context.xxx% (if any).
cleanExtraPlaceholders
php
zephir-lang/zephir
src/Logger/Formatter/CompilerFormatter.php
https://github.com/zephir-lang/zephir/blob/master/src/Logger/Formatter/CompilerFormatter.php
MIT
private function getFileContents(string $file): array { if (!isset($this->filesContent[$file])) { $this->filesContent[$file] = file_exists($file) ? file($file) : []; } return $this->filesContent[$file]; }
Gets the contents of the files that are involved in the log message.
getFileContents
php
zephir-lang/zephir
src/Logger/Formatter/CompilerFormatter.php
https://github.com/zephir-lang/zephir/blob/master/src/Logger/Formatter/CompilerFormatter.php
MIT