Skip to content

Commit 1a416e5

Browse files
committed
fixed bugs & patch Memory Leak
1 parent a046ab1 commit 1a416e5

File tree

8 files changed

+104
-2
lines changed

8 files changed

+104
-2
lines changed

demo/issue/10.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/monkeysuffrage/advanced_html_dom/issues/10
5+
*/
6+
7+
include_once dirname(__DIR__, 2) . '/vendor/autoload.php';
8+
9+
$dom = \Bavix\AdvancedHtmlDom\strGetHtml('<html><table><tr><td>1
10+
<td>2<tr><td>3<td></body></html>');
11+
12+
var_dump($dom->find('tr',0)->clean_text);

demo/issue/9.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/monkeysuffrage/advanced_html_dom/issues/9
5+
*/
6+
7+
include_once dirname(__DIR__, 2) . '/vendor/autoload.php';
8+
9+
$dom = \Bavix\AdvancedHtmlDom\strGetHtml('<body><p>x</p><p>y</p></body>');
10+
11+
echo $dom->find('p')[1];

src/AdvancedHtmlDom/AHTMLNode.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace Bavix\AdvancedHtmlDom;
44

5+
/**
6+
* Class AHTMLNode
7+
*
8+
* @package Bavix\AdvancedHtmlDom
9+
*
10+
* @property-read string $clean_text
11+
*/
512
class AHTMLNode extends AdvancedHtmlBase implements \ArrayAccess
613
{
714

@@ -24,6 +31,16 @@ public function __construct($node, $doc)
2431
$this->is_text = $node->nodeName === '#text';
2532
}
2633

34+
/**
35+
* @inheritdoc
36+
*/
37+
public function __destruct()
38+
{
39+
$this->_path = null;
40+
unset($this->_path);
41+
parent::__destruct();
42+
}
43+
2744
/**
2845
* @param $html
2946
*
@@ -209,10 +226,14 @@ public function offsetGet($offset)
209226
*/
210227
public function offsetSet($key, $value)
211228
{
229+
if (\in_array($key, ['_path','dom','doc','node']))
230+
{
231+
return;
232+
}
233+
212234
if ($value)
213235
{
214236
$this->node->setAttribute($key, $value);
215-
216237
return;
217238
}
218239

src/AdvancedHtmlDom/AHTMLNodeList.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public function __construct($nodeList, $doc)
3232
$this->doc = $doc;
3333
}
3434

35+
/**
36+
* @see https://github.com/monkeysuffrage/advanced_html_dom/issues/19
37+
*/
38+
public function __destruct()
39+
{
40+
$this->nodeList = $this->doc = null;
41+
unset($this->nodeList, $this->doc);
42+
Cleanup::all();
43+
}
44+
3545
/*
3646
abstract public boolean offsetExists ( mixed $offset )
3747
abstract public mixed offsetGet ( mixed $offset )

src/AdvancedHtmlDom/AdvancedHtmlBase.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ class AdvancedHtmlBase
2525
*/
2626
public $is_text = false;
2727

28+
/**
29+
* @see https://github.com/monkeysuffrage/advanced_html_dom/issues/19
30+
*/
31+
public function __destruct()
32+
{
33+
$this->doc = $this->dom = $this->node = null;
34+
unset($this->doc, $this->dom, $this->node);
35+
Cleanup::all();
36+
}
37+
2838
/**
2939
* @return mixed
3040
*/
@@ -203,7 +213,7 @@ public function __call($key, $args)
203213

204214
// attributes
205215
case 'hasattribute':
206-
return !$this->is_text && $this->node->getAttribute($args[0]);
216+
return !$this->is_text && $this->node->hasAttribute($args[0]);
207217

208218
case 'getattribute':
209219
$arg = $args[0];
@@ -268,6 +278,10 @@ public function __call($key, $args)
268278
return $this->$arg1($this->$arg2);
269279
}
270280

281+
if (\in_array($key, ['dom','node','doc'])) {
282+
return null;
283+
}
284+
271285
if (!\preg_match(ATTRIBUTE_REGEX, $key, $m))
272286
{
273287
\trigger_error('Unknown method or property: ' . $key, E_USER_WARNING);

src/AdvancedHtmlDom/AdvancedHtmlDom.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public function __construct($html = null, $is_xml = false)
3737
}
3838
}
3939

40+
/**
41+
* @inheritdoc
42+
*/
43+
public function __destruct()
44+
{
45+
$this->xpath = $this->root = null;
46+
unset($this->xpath, $this->root);
47+
parent::__destruct();
48+
}
49+
4050
/**
4151
* @param InterfaceCache $cache
4252
*/

src/AdvancedHtmlDom/CacheSystem/CacheStatic.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@
55
class CacheStatic implements InterfaceCache
66
{
77

8+
/**
9+
* @var array
10+
*/
811
protected static $cache = [];
912

13+
/**
14+
* @param $url
15+
*
16+
* @return mixed
17+
*/
1018
public function get($url)
1119
{
1220
if (!isset(self::$cache[$url]))

src/AdvancedHtmlDom/Cleanup.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bavix\AdvancedHtmlDom;
4+
5+
class Cleanup {
6+
public static function all(): void {
7+
if (\function_exists('gc_collect_cycles')) {
8+
\gc_collect_cycles();
9+
}
10+
11+
if (\function_exists('gc_mem_caches')) {
12+
\gc_mem_caches();
13+
}
14+
}
15+
}
16+

0 commit comments

Comments
 (0)