get all innerhtml of shadow root tree

The goal is to collect all the html of all shadow-roots. Even when a shadow is a tree containing several new once. I want to collect all it's html. Just like when you use F12 then you can also see all the source. So far I get till the root:

// An example of using php-webdriver.

namespace Facebook\WebDriver;

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;


$host = 'http://localhost:9515';//chromedriver.exe
$capabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);


// adding cookie

$cookie = new Cookie('cookie_name', 'cookie_value');

$cookies = $driver->manage()->getCookies();

// print the title of the current page
echo "The title is '" . $driver->getTitle() . "'\n";

var allNodes = document.getElementsByTagName(\'*\');
for (var i = 0; i < allNodes.length; i++) {
  if(allNodes[i].shadowRoot) {
    //alert(\'it works\'+i+allNodes[i].shadowRoot.innerHTML); 


I get the code of the shadow root, but that one contains 3 more shadow-root's. How do I collect those and have the sourcecode of it.