Skip to content

Commit

Permalink
Merge pull request #2309 from zephir-lang/#2308-globals-string
Browse files Browse the repository at this point in the history
#2308 - Fix support of `string` type in struct globals
  • Loading branch information
Jeckerson authored Oct 8, 2021
2 parents e147e70 + 4ce4cda commit a51113c
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format based on [Keep a Changelog](http://keepachangelog.com)
and this project adheres to [Semantic Versioning](http://semver.org).

## [Unreleased]
### Fixed
- Fix support of `string` type in struct globals [#2308](https://github.com/zephir-lang/zephir/issues/2308)

## [0.15.0] - 2021-10-05
### Added
Expand Down
6 changes: 6 additions & 0 deletions Library/Code/Builder/Struct.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public function getCDefault(string $name, array $global, string $namespace): str
case 'bool':
return '';

case 'string':
return "\t".$namespace.'_globals->'.$this->simpleName.'.'.$name.' = ZSTR_VAL(zend_string_init(ZEND_STRL("'.$global['default'].'"), 0));';

case 'int':
case 'uint':
case 'long':
Expand Down Expand Up @@ -192,6 +195,9 @@ protected function convertToCType(string $type): string
case 'hash':
return 'HashTable* ';

case 'string':
return 'zend_string* ';

case 'int':
case 'uint':
case 'long':
Expand Down
19 changes: 9 additions & 10 deletions Library/Compiler/CompilerFileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir\Compiler;

use Psr\Log\LoggerInterface;
Expand All @@ -19,15 +21,12 @@

final class CompilerFileFactory
{
private $config;
private $filesystem;
private $logger;

public function __construct(
Config $config,
FileSystemInterface $filesystem,
LoggerInterface $logger
) {
private Config $config;
private FileSystemInterface $filesystem;
private LoggerInterface $logger;

public function __construct(Config $config, FileSystemInterface $filesystem, LoggerInterface $logger)
{
$this->config = $config;
$this->filesystem = $filesystem;
$this->logger = $logger;
Expand All @@ -43,7 +42,7 @@ public function __construct(
*
* @return FileInterface
*/
public function create($className, $filePath)
public function create(string $className, string $filePath): FileInterface
{
$compiler = new CompilerFile($this->config, new AliasManager(), $this->filesystem);

Expand Down
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
"type": "bool",
"default": true
},
"orm.cache_prefix": {
"type": "string",
"default": "prefix-string-"
},
"extension.test_ini_variable": {
"type": "bool",
"default": true,
Expand Down
1 change: 1 addition & 0 deletions ext/php_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef struct _zephir_struct_db {
typedef struct _zephir_struct_orm {
int cache_level;
zend_bool cache_enable;
zend_string* cache_prefix;
} zephir_struct_orm;

typedef struct _zephir_struct_extension {
Expand Down
2 changes: 2 additions & 0 deletions ext/stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ PHP_INI_BEGIN()


STD_PHP_INI_BOOLEAN("stub.orm.cache_enable", "1", PHP_INI_ALL, OnUpdateBool, orm.cache_enable, zend_stub_globals, stub_globals)

STD_PHP_INI_BOOLEAN("extension.test_ini_variable", "1", PHP_INI_ALL, OnUpdateBool, extension.test_ini_variable, zend_stub_globals, stub_globals)
STD_PHP_INI_BOOLEAN("ini-entry.my_setting_1", "1", PHP_INI_ALL, OnUpdateBool, my_setting_1, zend_stub_globals, stub_globals)
STD_PHP_INI_BOOLEAN("stub.test_setting_1", "1", PHP_INI_ALL, OnUpdateBool, test_setting_1, zend_stub_globals, stub_globals)
Expand Down Expand Up @@ -520,6 +521,7 @@ static void php_zephir_init_globals(zend_stub_globals *stub_globals)
stub_globals->db.my_setting_3 = 7.5;
stub_globals->orm.cache_level = 3;

stub_globals->orm.cache_prefix = ZSTR_VAL(zend_string_init(ZEND_STRL("prefix-string-"), 0));

stub_globals->my_setting_1 = 1;
stub_globals->test_setting_1 = 1;
Expand Down
37 changes: 37 additions & 0 deletions ext/stub/globals.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ext/stub/globals.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions stub/globals.zep
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class Globals
globals_set("orm.cache_level", value);
}

public function setDefaultGlobalsOrmCachePrefix(string value) -> void
{
globals_set("orm.cache_prefix", value);
}

/* Get Default Properties */

/**
Expand Down Expand Up @@ -108,4 +113,12 @@ class Globals
{
return globals_get("orm.cache_level");
}

/**
* @return mixed
*/
public function getDefaultGlobalsOrmCachePrefix()
{
return globals_get("orm.cache_prefix");
}
}
17 changes: 17 additions & 0 deletions tests/Extension/GlobalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ public function testSetStringValue(): void

$this->test->setStringValue('Long Text without any sense...');
$this->assertSame('Long Text without any sense...', $this->test->getDefaultGlobals8());

/**
* Get and set string value from globals struct.
*/
$this->assertSame('prefix-string-', $this->test->getDefaultGlobalsOrmCachePrefix());

$this->test->setDefaultGlobalsOrmCachePrefix('1');
$this->assertSame('1', $this->test->getDefaultGlobalsOrmCachePrefix());

$this->test->setDefaultGlobalsOrmCachePrefix('c');
$this->assertSame('c', $this->test->getDefaultGlobalsOrmCachePrefix());

$this->test->setDefaultGlobalsOrmCachePrefix('char');
$this->assertSame('char', $this->test->getDefaultGlobalsOrmCachePrefix());

$this->test->setDefaultGlobalsOrmCachePrefix('Long Text without any sense...');
$this->assertSame('Long Text without any sense...', $this->test->getDefaultGlobalsOrmCachePrefix());
}

public function testSetBoolValueUsingInt(): void
Expand Down

0 comments on commit a51113c

Please sign in to comment.