diff --git a/README.md b/README.md index cb50287..9c3c056 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,10 @@ Use composer ```composer require willwashburn/mushroom``` -Alternatively, add ```"willwashburn/mushroom": "~2.10"``` to your composer.json +Alternatively, add ```"willwashburn/mushroom": "~2.11"``` to your composer.json ## Change Log +- v2.11.0 - Expose HttpStatusCode for urls when searching canonically - v2.10.0 - Add method to add a domain to JS Redirect Domains array - v2.9.1 - Fix bug where some html sources were not cached - v2.9.0 - Expose HTML for urls when searching canonically diff --git a/src/mushroom/Mushroom.php b/src/mushroom/Mushroom.php index 6059fca..575ce13 100644 --- a/src/mushroom/Mushroom.php +++ b/src/mushroom/Mushroom.php @@ -45,6 +45,15 @@ class Mushroom */ private $html = []; + /** + * Some user-land use cases popped up that required the response_code of the + * eventual curl request we made when finding canonical urls. We expose a + * method to get this content in the event someone needs it. + * + * @var int[] + */ + private $httpStatusCode = []; + /** * Mushroom constructor. * @@ -147,6 +156,22 @@ public function getCachedHtml($url) return false; } + + /** + * We occasionally can find use in knowing the http status code + * + * @param $url + * + * @return bool + */ + public function getCachedHttpStatusCode($url) + { + if ($this->httpStatusCode[$url]) { + return $this->httpStatusCode[$url]; + } + return false; + } + /** * @param array $urls The urls to unfurl * @param array $curlOptions Custom curl options that can be passed in @@ -202,6 +227,10 @@ private function batchFollow($urls, $curlOptions, $followHttpRefresh, $findCanon if ($url['html']) { $this->html[$urls[$key]] = $url['html']; } + + if ($url['http_status_code']) { + $this->httpStatusCode[$urls[$key]] = $url['http_status_code']; + } } if ($followHttpRefresh && count($retries) > 0) { @@ -342,9 +371,10 @@ private function getUrlFromHandle($ch, $followHttpRefresh, $findCanonical) $url = $this->ensureSchemeAndHost($ch, $url); return [ - 'refresh' => true, - 'url' => $url, - 'html' => $html, + 'refresh' => true, + 'url' => $url, + 'html' => $html, + 'http_status_code' => $httpStatusCode, ]; } } @@ -366,17 +396,19 @@ private function getUrlFromHandle($ch, $followHttpRefresh, $findCanonical) $url = $this->ensureSchemeAndHost($ch, $url); return [ - 'refresh' => false, - 'url' => $url, - 'html' => $html + 'refresh' => false, + 'url' => $url, + 'html' => $html, + 'http_status_code' => $httpStatusCode, ]; } } return [ - 'refresh' => false, - 'url' => $effectiveUrl, - 'html' => $html, + 'refresh' => false, + 'url' => $effectiveUrl, + 'html' => $html, + 'http_status_code' => $httpStatusCode, ]; } diff --git a/tests/ExpandLinksTest.php b/tests/ExpandLinksTest.php index 1431c96..312a86e 100644 --- a/tests/ExpandLinksTest.php +++ b/tests/ExpandLinksTest.php @@ -127,11 +127,11 @@ public function test_setting_curl_options_works() ->shouldReceive('curl_getinfo')->getMock() ->shouldReceive('curl_setopt_array') ->with(M::any(), M::on(function ($arg) use ($expected_curl_opts) { - foreach ( array_keys($expected_curl_opts) as $key ) { - if ( $arg[$key] != $expected_curl_opts[$key] ) { - return false; - } - } + foreach (array_keys($expected_curl_opts) as $key) { + if ($arg[$key] != $expected_curl_opts[$key]) { + return false; + } + } return true; })) @@ -151,7 +151,7 @@ public function test_without_follow_http_redirects() ]; $mushroom = new Mushroom(); - foreach ( $links as list( $link, $expected_result ) ) { + foreach ($links as list( $link, $expected_result )) { $result = $mushroom->expand($link, [], false); $this->assertEquals($expected_result, $result); @@ -164,15 +164,17 @@ public function test_get_html_works() ['http://bit.ly/1bdDlXc', 'https://www.google.com/'], ['http://www.tailwindapp.com', 'https://www.tailwindapp.com/'], ['https://diply.com/article/auntyacid/pinterest-diy-easy-solutions', 'https://diply.com/article/auntyacid/pinterest-diy-easy-solutions'], + ['https://www.zazzle.com/cookie_monster_cookies_for_santa_dinner_plate-115773106232089655', 'https://www.zazzle.com/cookie_monster_cookies_for_santa_dinner_plate-115773106232089655'], ]; $mushroom = new Mushroom(); - foreach ( $links as list( $link, $expected_result ) ) { + foreach ($links as list( $link, $expected_result )) { $result = $mushroom->canonical($link); $this->assertEquals($expected_result, $result); $this->assertNotFalse($mushroom->getCachedHtml($link), $link); + $this->assertNotEmpty($mushroom->getCachedHttpStatusCode($link), $link); } $this->assertFalse($mushroom->getCachedHtml('https://www.tailwindapp.com'));