sunra / php-simple-html-dom-parser

PHP Simple HTML DOM Parser adaptation for Composer and PSR-0
1.3k stars 352 forks source link

Endless __destruct() loop when served via Laravels php artisan serve command #60

Open dusekdan opened 6 years ago

dusekdan commented 6 years ago

When I use your library together with Laravel and take advantage of Laravel's possibility to start local development server using command php artisan serve I run into an issue where Laravel server gets stuck in an endless loop of calls to simple_html_dom_node->__destruct(). After maximum execution time is exceeded, Laravel server calls:

Laravel development server started: <http://127.0.0.1:8000>
[Thu Jun 14 09:24:43 2018] PHP Fatal error:  Maximum execution time of 60 seconds exceeded in C:\Users\[REDACTED]\Desktop\Tests\PHP\blog\blog\vendor\sunra\php-simple-html-dom-parser\Src\Sunra\PhpSimple\simplehtmldom_1_5\simple_html_dom.php on line 140
[Thu Jun 14 09:24:43 2018] PHP Stack trace:
[Thu Jun 14 09:24:43 2018] PHP   1. simplehtmldom_1_5\simple_html_dom_node->__destruct() C:\Users\[REDACTED]\Desktop\Tests\PHP\blog\blog\vendor\sunra\php-simple-html-dom-parser\Src\Sunra\PhpSimple\simplehtmldom_1_5\simple_html_dom.php:0

I debugged the issue for a while but could not resolve it by any other way than to delete/rename/comment-out your destructors in mentioned class.

Minimum, Complete and Verifiable example/Steps to reproduce:

  1. Create Laravel project by running composer create-project --prefer-dist laravel/laravel blog
  2. cd blog and update composer.json with required dependency to your library "sunra/php-simple-html-dom-parser": "^1.5"
  3. Run composer update to fetch newly added dependency.
  4. Open file routes/web.php and update its contents to contain following

    use Sunra\PhpSimple\HtmlDomParser;
    Route::get('/',  function(){
    $input = <<<EOM
    <!-- PUT YOUR NON-TRIVIAL HTML MARKUP HERE -->                    
    EOM;
    $parser = new HtmlDomParser();
    $dom = $parser->str_get_html($input);
    
    return view('welcome');
    });

    (1) Note that <!-- YOUR NON-TRIVIAL HTML MARKUP HERE --> should really be replaced with non-trivial markup, e.g. google.com's source from front-page.

  5. Start local development server php artisan serve and access used address (it defaults to 127.0.0.1:8000)

(2) Note I was not able to reproduce it using on PHPv7.1.14 or PHPv7.1, but PHPv7.1.13, PHPv7.1.18 and even PHPv7.2 do suffer from this behavior.

I worked-around this issue by setting up composer script on post-autoload-dump event where I search and destroy (rename) your destructors.

alecgeatches commented 5 years ago

I'm having the same problem with an endless loop of destructors with HtmlDomParser from WordPress on PHP 7.1.13. The workaround I found was to manually call ->clear() on the DOM element before returning from a function:

function modify_content($content) {
    $dom = HtmlDomParser::str_get_html($content);
    // ...

    // Add the line below
    $dom->clear();

    return $content;
}