Skip to content

Commit 2a5fc83

Browse files
committed
Rework handling of iterator as result of database query
1 parent c67e513 commit 2a5fc83

8 files changed

+95
-69
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ For guidance on using the plugin see the plugin's page within the phplist docume
4949
## Version history ##
5050

5151
version Description
52+
3.34.0+20250202 Add methods to disable and enable a subscriber
5253
3.33.1+20240819 Make the legacy CommonPlugin_xxx classes aliases of the namespaced classes
5354
3.33.0+20240215 Add trait for listuser table
5455
3.32.1+20231226 Use an autoload function to load legacy classes instead of single class file

changelog.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
version 3.34.0+20250202
2+
Avoid dynamic property
3+
Update curl user agent
4+
Add methods to disable and enable a subscriber
5+
Rework handling of iterator as result of database query
6+
17
version 3.33.1+20240819
28
Make the legacy CommonPlugin_xxx classes aliases of the namespaced classes
39
CS Fixer

plugins/Common/CountableIterator.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace phpList\plugin\Common;
4+
5+
/**
6+
* CommonPlugin for phplist.
7+
*
8+
* This file is a part of CommonPlugin.
9+
*
10+
* @category phplist
11+
*
12+
* @author Duncan Cameron
13+
* @copyright 2025 Duncan Cameron
14+
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, Version 3
15+
*/
16+
class CountableIterator extends \IteratorIterator implements \Countable
17+
{
18+
private $count;
19+
20+
/**
21+
* @param Iterator $iterator
22+
* @param int $count
23+
*/
24+
public function __construct($iterator, $count)
25+
{
26+
parent::__construct($iterator);
27+
$this->count = $count;
28+
}
29+
30+
/**
31+
* Implementation of Countable interface.
32+
* Returns the number of rows in the result.
33+
*
34+
* @return int
35+
*/
36+
#[\ReturnTypeWillChange]
37+
public function count()
38+
{
39+
return $this->count;
40+
}
41+
}

plugins/Common/DB.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,16 @@ public function queryAffectedRows($sql)
9494
* @param string $sql the query
9595
* @param string $keyColumn optional result column to use as the key
9696
*
97-
* @return DBResultIterator iterator
97+
* @return CountableIterator iterator
9898
*/
9999
public function queryAll($sql, $keyColumn = null)
100100
{
101101
$resource = $this->_query($sql);
102102
$count = Sql_Num_Rows($resource);
103103

104-
return new DBResultIterator($resource, $count, $keyColumn);
104+
return $keyColumn === null
105+
? new CountableIterator($resource, $count)
106+
: new CountableIterator(new KeyedIterator($resource, $keyColumn), $count);
105107
}
106108

107109
/**

plugins/Common/DBResultIterator.php

-65
This file was deleted.

plugins/Common/KeyedIterator.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace phpList\plugin\Common;
4+
5+
/**
6+
* CommonPlugin for phplist.
7+
*
8+
* This file is a part of CommonPlugin.
9+
*
10+
* @category phplist
11+
*
12+
* @author Duncan Cameron
13+
* @copyright 2025 Duncan Cameron
14+
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, Version 3
15+
*/
16+
class KeyedIterator extends \IteratorIterator
17+
{
18+
private $keyColumn;
19+
20+
/**
21+
* @param Iterator $iterator
22+
* @param string $keyColumn
23+
*/
24+
public function __construct($iterator, $keyColumn)
25+
{
26+
parent::__construct($iterator);
27+
$this->keyColumn = $keyColumn;
28+
}
29+
30+
/**
31+
* Return the key as a column from the result.
32+
*
33+
* @return mixed
34+
*/
35+
#[\ReturnTypeWillChange]
36+
public function key()
37+
{
38+
return parent::current()[$this->keyColumn];
39+
}
40+
}

plugins/CommonPlugin/class_map.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'phpList\plugin\Common\Controller' => $pluginsDir . '/Common/Controller.php',
1414
'phpList\plugin\Common\ControllerFactory' => $pluginsDir . '/Common/ControllerFactory.php',
1515
'phpList\plugin\Common\ControllerFactoryBase' => $pluginsDir . '/Common/ControllerFactoryBase.php',
16+
'phpList\plugin\Common\CountableIterator' => $pluginsDir . '/Common/CountableIterator.php',
1617
'phpList\plugin\Common\DAO' => $pluginsDir . '/Common/DAO.php',
1718
'phpList\plugin\Common\DAO\Attribute' => $pluginsDir . '/Common/DAO/Attribute.php',
1819
'phpList\plugin\Common\DAO\AttributeTrait' => $pluginsDir . '/Common/DAO/AttributeTrait.php',
@@ -27,7 +28,6 @@
2728
'phpList\plugin\Common\DAO\User' => $pluginsDir . '/Common/DAO/User.php',
2829
'phpList\plugin\Common\DAO\UserTrait' => $pluginsDir . '/Common/DAO/UserTrait.php',
2930
'phpList\plugin\Common\DB' => $pluginsDir . '/Common/DB.php',
30-
'phpList\plugin\Common\DBResultIterator' => $pluginsDir . '/Common/DBResultIterator.php',
3131
'phpList\plugin\Common\DatabaseCache' => $pluginsDir . '/Common/DatabaseCache.php',
3232
'phpList\plugin\Common\Exception' => $pluginsDir . '/Common/Exception.php',
3333
'phpList\plugin\Common\ExportCSV' => $pluginsDir . '/Common/ExportCSV.php',
@@ -46,6 +46,7 @@
4646
'phpList\plugin\Common\IMailClient' => $pluginsDir . '/Common/IMailClient.php',
4747
'phpList\plugin\Common\IPopulator' => $pluginsDir . '/Common/IPopulator.php',
4848
'phpList\plugin\Common\ImageTag' => $pluginsDir . '/Common/ImageTag.php',
49+
'phpList\plugin\Common\KeyedIterator' => $pluginsDir . '/Common/KeyedIterator.php',
4950
'phpList\plugin\Common\Listing' => $pluginsDir . '/Common/Listing.php',
5051
'phpList\plugin\Common\Logger' => $pluginsDir . '/Common/Logger.php',
5152
'phpList\plugin\Common\MailSender' => $pluginsDir . '/Common/MailSender.php',

plugins/CommonPlugin/version.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.33.1+20240819
1+
3.34.0+20250202

0 commit comments

Comments
 (0)