Zephir\Compiler::processExtensionGlobals PHP Method

processExtensionGlobals() public method

Process extension globals
public processExtensionGlobals ( string $namespace ) : array
$namespace string
return array
    public function processExtensionGlobals($namespace)
    {
        $globalCode = '';
        $globalStruct = '';
        $globalsDefault = array(array(), array());
        $initEntries = array();
        /**
         * Generate the extensions globals declaration
         */
        $globals = $this->config->get('globals');
        if (is_array($globals)) {
            $structures = array();
            $variables = array();
            foreach ($globals as $name => $global) {
                $parts = explode('.', $name);
                if (isset($parts[1])) {
                    $structures[$parts[0]][$parts[1]] = $global;
                } else {
                    $variables[$parts[0]] = $global;
                }
            }
            /**
             * Process compound structures
             */
            foreach ($structures as $structureName => $internalStructure) {
                if (preg_match('/^[0-9a-zA-Z\\_]$/', $structureName)) {
                    throw new Exception("Struct name: '" . $structureName . "' contains invalid characters");
                }
                $structBuilder = new Code\Builder\Struct('_zephir_struct_' . $structureName, $structureName);
                foreach ($internalStructure as $field => $global) {
                    if (preg_match('/^[0-9a-zA-Z\\_]$/', $field)) {
                        throw new Exception("Struct field name: '" . $field . "' contains invalid characters");
                    }
                    $structBuilder->addProperty($field, $global);
                    $isModuleGlobal = (int) (!empty($global['module']));
                    $globalsDefault[$isModuleGlobal][] = $structBuilder->getCDefault($field, $global, $namespace) . PHP_EOL;
                    $initEntries[] = $structBuilder->getInitEntry($field, $global, $namespace);
                }
                $globalStruct .= $structBuilder . PHP_EOL;
            }
            $globalCode = PHP_EOL;
            foreach ($structures as $structureName => $internalStructure) {
                $globalCode .= "\t" . 'zephir_struct_' . $structureName . ' ' . $structureName . ';' . PHP_EOL . PHP_EOL;
            }
            /**
             * Process single variables
             */
            foreach ($variables as $name => $global) {
                if (preg_match('/^[0-9a-zA-Z\\_]$/', $name)) {
                    throw new Exception("Extension global variable name: '" . $name . "' contains invalid characters");
                }
                if (!isset($global['default'])) {
                    throw new Exception("Extension global variable name: '" . $name . "' contains invalid characters");
                }
                $isModuleGlobal = (int) (!empty($global['module']));
                $type = $global['type'];
                switch ($global['type']) {
                    case 'boolean':
                    case 'bool':
                        $type = 'zend_bool';
                        if ($global['default'] === true) {
                            $globalsDefault[$isModuleGlobal][] = "\t" . $namespace . '_globals->' . $name . ' = 1;' . PHP_EOL;
                        } else {
                            $globalsDefault[$isModuleGlobal][] = "\t" . $namespace . '_globals->' . $name . ' = 0;' . PHP_EOL;
                        }
                        break;
                    case 'int':
                    case 'uint':
                    case 'long':
                    case 'double':
                        $globalsDefault[$isModuleGlobal][] = "\t" . $namespace . '_globals->' . $name . ' = ' . $global['default'] . ';' . PHP_EOL;
                        break;
                    case 'char':
                    case 'uchar':
                        $globalsDefault[$isModuleGlobal][] = "\t" . $namespace . '_globals->' . $name . ' = \'' . $global['default'] . '\'";' . PHP_EOL;
                        break;
                    default:
                        throw new Exception("Unknown type '" . $global['type'] . "' for extension global '" . $name . "'");
                }
                $globalCode .= "\t" . $type . ' ' . $name . ';' . PHP_EOL . PHP_EOL;
                $iniEntry = array();
                if (isset($global['ini-entry'])) {
                    $iniEntry = $global['ini-entry'];
                }
                if (!isset($iniEntry['name'])) {
                    $iniName = $name;
                } else {
                    $iniName = $iniEntry['name'];
                }
                if (!isset($iniEntry['scope'])) {
                    $scope = 'PHP_INI_ALL';
                } else {
                    $scope = $iniEntry['scope'];
                }
                switch ($global['type']) {
                    case 'boolean':
                    case 'bool':
                        $initEntries[] = 'STD_PHP_INI_BOOLEAN("' . $iniName . '", "' . (int) ($global['default'] === true) . '", ' . $scope . ', OnUpdateBool, ' . $name . ', zend_' . $namespace . '_globals, ' . $namespace . '_globals)';
                        break;
                }
            }
        }
        $globalsDefault[0] = implode('', $globalsDefault[0]);
        $globalsDefault[1] = implode('', $globalsDefault[1]);
        return array($globalCode, $globalStruct, $globalsDefault, $initEntries);
    }