Skip to content

Commit

Permalink
Merge pull request #122 from moufmouf/weakref-php7.4
Browse files Browse the repository at this point in the history
Adding support for native WeakReferences
  • Loading branch information
moufmouf authored Jul 12, 2020
2 parents 9f9dac2 + 415d7da commit 12eaeca
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 11 deletions.
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ cache:
- $HOME/.composer/cache
matrix:
include:
- php: 7.4
env: PREFER_LOWEST="" DB=mysql RUN_PHPSTAN=1 RUN_CSCHECK=1 RUN_REQUIRECHECKER=1 NO_WEAKREF=1
services:
- mysql
- php: 7.2
env: PREFER_LOWEST="" DB=mysql RUN_PHPSTAN=1 RUN_CSCHECK=1 RUN_REQUIRECHECKER=1
env: PREFER_LOWEST="" DB=mysql
services:
- mysql
- php: 7.3
Expand Down Expand Up @@ -57,7 +61,10 @@ matrix:
postgresql: "9.6"
services:
- postgresql
- php: 7.3
env: PREFER_LOWEST="" DB=mysql NO_WEAKREF=1
allow_failures:
- php: 7.3 # 7.3 allowed to fail due to an issue with greenlion/PHP-SQL-Parser: https://github.com/greenlion/PHP-SQL-Parser/pull/304
- php: 7.1
env: PREFER_LOWEST="" DB=oracle PHPUNITFILE="-c phpunit.oracle.xml"
- php: 7.1
Expand Down
2 changes: 1 addition & 1 deletion composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"null", "true", "false",
"static", "self", "parent",
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "object",
"WeakRef",
"WeakRef", "WeakReference",
"TheCodingMachine\\TDBM\\Fixtures\\Interfaces\\TestUserDaoInterface",
"TheCodingMachine\\TDBM\\Fixtures\\Traits\\TestUserDaoTrait",
"TheCodingMachine\\TDBM\\Fixtures\\Interfaces\\TestUserInterface",
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ parameters:
- "#expects array<string>, array<int, int|string> given.#"
- "/Parameter #. \\$types of method Doctrine\\\\DBAL\\\\Connection::.*() expects array<int|string>, array<int, Doctrine\\\\DBAL\\\\Types\\\\Type> given./"
- "#Method TheCodingMachine\\\\TDBM\\\\Schema\\\\ForeignKey::.*() should return .* but returns array<string>|string.#"
- '#Method TheCodingMachine\\TDBM\\NativeWeakrefObjectStorage::get\(\) should return TheCodingMachine\\TDBM\\DbRow\|null but returns object\|null.#'
-
message: '#WeakRef#'
path: src/WeakrefObjectStorage.php
-
message: '#Result of && is always false.#'
path: src/Test/Dao/Bean/Generated/ArticleBaseBean.php
Expand Down
107 changes: 107 additions & 0 deletions src/NativeWeakrefObjectStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);

/*
Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

namespace TheCodingMachine\TDBM;

use WeakReference;

/**
* The NativeWeakrefObjectStorage class is used to reference all beans that have been fetched from the database.
* If a bean is requested twice from TDBM, the NativeWeakrefObjectStorage is used to "cache" the bean.
* Unlike the StandardObjectStorage, the NativeWeakrefObjectStorage manages memory in a clever way, using WeakReference.
* WeakReference have been added in PHP 7.4.
*
* @author David Negrier
*/
class NativeWeakrefObjectStorage implements ObjectStorageInterface
{
/**
* An array of fetched object, accessible via table name and primary key.
* If the primary key is split on several columns, access is done by an array of columns, serialized.
*
* @var WeakReference[][]
*/
private $objects = array();

/**
* Every 10000 set in the dataset, we perform a cleanup to ensure the WeakRef instances
* are removed if they are no more valid.
* This is to avoid having memory used by dangling WeakRef instances.
*
* @var int
*/
private $garbageCollectorCount = 0;

/**
* Sets an object in the storage.
*
* @param string $tableName
* @param string|int $id
* @param DbRow $dbRow
*/
public function set(string $tableName, $id, DbRow $dbRow): void
{
$this->objects[$tableName][$id] = WeakReference::create($dbRow);
++$this->garbageCollectorCount;
if ($this->garbageCollectorCount === 10000) {
$this->garbageCollectorCount = 0;
$this->cleanupDanglingWeakRefs();
}
}

/**
* Returns an object from the storage (or null if no object is set).
*
* @param string $tableName
* @param string|int $id
*
* @return DbRow|null
*/
public function get(string $tableName, $id) : ?DbRow
{
if (isset($this->objects[$tableName][$id])) {
return $this->objects[$tableName][$id]->get();
}
return null;
}

/**
* Removes an object from the storage.
*
* @param string $tableName
* @param string|int $id
*/
public function remove(string $tableName, $id): void
{
unset($this->objects[$tableName][$id]);
}

private function cleanupDanglingWeakRefs(): void
{
foreach ($this->objects as $tableName => $table) {
foreach ($table as $id => $obj) {
if ($obj->get() === null) {
unset($this->objects[$tableName][$id]);
}
}
}
}
}
9 changes: 6 additions & 3 deletions src/TDBMService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace TheCodingMachine\TDBM;

use function class_exists;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\ClearableCache;
use Doctrine\Common\Cache\VoidCache;
Expand All @@ -46,7 +47,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use function var_export;
use WeakReference;

/**
* The TDBMService class is the main TDBM class. It provides methods to retrieve TDBMObject instances
Expand Down Expand Up @@ -101,7 +102,7 @@ class TDBMService
* Access is done by table name and then by primary key.
* If the primary key is split on several columns, access is done by an array of columns, serialized.
*
* @var StandardObjectStorage|WeakrefObjectStorage
* @var ObjectStorageInterface
*/
private $objectStorage;

Expand Down Expand Up @@ -179,7 +180,9 @@ class TDBMService
*/
public function __construct(ConfigurationInterface $configuration)
{
if (extension_loaded('weakref')) {
if (class_exists(WeakReference::class)) {
$this->objectStorage = new NativeWeakrefObjectStorage();
} elseif (extension_loaded('weakref')) {
$this->objectStorage = new WeakrefObjectStorage();
} else {
$this->objectStorage = new StandardObjectStorage();
Expand Down
4 changes: 3 additions & 1 deletion src/WeakrefObjectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
* If a bean is requested twice from TDBM, the WeakrefObjectStorage is used to "cache" the bean.
* Unlike the StandardObjectStorage, the WeakrefObjectStorage manages memory in a clever way, using the weakref
* PHP extension. It is used if the "weakref" extension is available.
* Otherwise, the StandardObjectStorage is used instead.
* Otherwise, the StandardObjectStorage or NativeWeakrefObjectStorage (in PHP 7.4+) is used instead.
*
* Note: the weakref extension is available until PHP 7.2, but not available in PHP 7.3+
*
* @author David Negrier
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Commands/GenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testCall(): void

$result = $this->callCommand(new GenerateCommand($this->getConfiguration()), $input);

$this->assertStringContainsString('Finished regenerating DAOs and beans', $result);
$this->assertContains('Finished regenerating DAOs and beans', $result);
}

/**
Expand Down
11 changes: 7 additions & 4 deletions tests/TDBMDaoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Ramsey\Uuid\Uuid;
use ReflectionClass;
use ReflectionMethod;
use ReflectionNamedType;
use TheCodingMachine\TDBM\Dao\TestAlbumDao;
use TheCodingMachine\TDBM\Dao\TestArticleDao;
use TheCodingMachine\TDBM\Dao\TestCountryDao;
Expand Down Expand Up @@ -88,6 +89,7 @@
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
use TheCodingMachine\TDBM\Utils\TDBMDaoGenerator;
use Symfony\Component\Process\Process;
use function gettype;

class TDBMDaoGeneratorTest extends TDBMAbstractServiceTest
{
Expand Down Expand Up @@ -1611,9 +1613,10 @@ public function testUuidv4(): void
public function testTypeHintedConstructors(): void
{
$userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct');
/** @var ReflectionNamedType $nameParam */
$nameParam = $userBaseBeanReflectionConstructor->getParameters()[0];

$this->assertSame('string', (string)$nameParam->getType());
$this->assertSame('string', $nameParam->getType()->getName());
}

/**
Expand Down Expand Up @@ -1725,7 +1728,7 @@ public function testBlob(): void
$resource = $loadedFile->getFile();
$result = fseek($resource, 0);
$this->assertSame(0, $result);
$this->assertIsResource($resource);
$this->assertSame('resource', gettype($resource));
$firstLine = fgets($resource);
$this->assertSame("<?php\n", $firstLine);
}
Expand All @@ -1739,7 +1742,7 @@ public function testReadBlob(): void
$loadedFile = $fileDao->getById(1);

$resource = $loadedFile->getFile();
$this->assertIsResource($resource);
$this->assertSame('resource', gettype($resource));
$firstLine = fgets($resource);
$this->assertSame("<?php\n", $firstLine);

Expand Down Expand Up @@ -1826,7 +1829,7 @@ public function testFilterBag(): void
public function testDecimalIsMappedToString(): void
{
$reflectionClass = new \ReflectionClass(BoatBaseBean::class);
$this->assertSame('string', (string) $reflectionClass->getMethod('getLength')->getReturnType());
$this->assertSame('string', $reflectionClass->getMethod('getLength')->getReturnType()->getName());
}

/**
Expand Down

0 comments on commit 12eaeca

Please sign in to comment.