Skip to content

Commit

Permalink
Added static methods for quick access (HtmlExplorer)
Browse files Browse the repository at this point in the history
  • Loading branch information
secondtruth committed Dec 17, 2014
1 parent 8327099 commit 4e26b1d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
37 changes: 37 additions & 0 deletions lib/HtmlExplorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,41 @@ public function getDOM()
{
return $this->dom;
}

/**
* Loads HTML from webpage.
*
* @param $url The URL of the webpage
* @param \FlameCore\Webtools\HttpClient $http The HttpClient instance to use (optional)
* @return \FlameCore\Webtools\HtmlExplorer
* @throws \RuntimeException if the URL could not be loaded.
*/
public static function fromWeb($url, HttpClient $http = null)
{
$http = $http ?: new HttpClient();

$request = $http->get($url);

if (!$request->success)
throw new \RuntimeException(sprintf('The URL "%s" could not be loaded.', $url));

return new self($request->data);
}

/**
* Loads HTML from file.
*
* @param $filename The name of the file
* @return \FlameCore\Webtools\HtmlExplorer
* @throws \LogicException if the file could not be opened.
*/
public static function fromFile($filename)
{
if (!is_file($filename) || !is_readable($filename))
throw new \LogicException(sprintf('The file "%s" does not exist or is not readable.', $filename));

$html = file_get_contents($filename);

return new self($html);
}
}
9 changes: 2 additions & 7 deletions lib/WebpageAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class WebpageAnalyzer
* Creates a WebpageAnalyzer object.
*
* @param string $url The URL of the webpage
* @throws \UnexpectedValueException if the URL could not be loaded.
* @throws \RuntimeException if the URL could not be loaded.
*/
public function __construct($url)
{
Expand All @@ -81,12 +81,7 @@ public function __construct($url)
$this->localUrl = preg_replace('#^(https?://.+)/.+#', '\1', $this->url);

$http = new HttpClient();
$request = $http->get($this->url);

if (!$request->success)
throw new \UnexpectedValueException(sprintf('The URL "%s" could not be loaded.', $url));

$html = new HtmlExplorer($request->data);
$html = HtmlExplorer::fromWeb($url, $http);

$node = $html->findFirstTag('base');
if ($node && $href = $node->getAttribute('href'))
Expand Down

0 comments on commit 4e26b1d

Please sign in to comment.