├── .gitignore ├── .php_cs.dist ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── favicon-extractor.php ├── phpunit.xml.dist ├── src ├── Exception │ ├── FaviconCouldNotBeSavedException.php │ └── InvalidUrlException.php ├── Facades │ └── FaviconExtractor.php ├── Favicon │ ├── Favicon.php │ ├── FaviconFactory.php │ ├── FaviconFactoryInterface.php │ └── FaviconInterface.php ├── FaviconExtractor.php ├── FaviconExtractorInterface.php ├── FaviconExtractorServiceProvider.php ├── Generator │ ├── FilenameGenerator.php │ └── FilenameGeneratorInterface.php └── Provider │ ├── DuckDuckGoProvider.php │ ├── GitHubProvider.php │ ├── GoogleProvider.php │ └── ProviderInterface.php └── tests ├── Favicon ├── FaviconFactoryTest.php └── FaviconTest.php ├── FaviconExtractorTest.php └── Generator └── FilenameGeneratorTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | phpunit.xml 3 | vendor/ 4 | -------------------------------------------------------------------------------- /.php_cs.dist: -------------------------------------------------------------------------------- 1 | name('*.php') 7 | ->in(__DIR__.DIRECTORY_SEPARATOR.'src') 8 | ->in(__DIR__.DIRECTORY_SEPARATOR.'tests') 9 | ; 10 | 11 | return Config::create() 12 | ->setUsingCache(false) 13 | ->setRiskyAllowed(true) 14 | ->setRules([ 15 | '@Symfony' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'linebreak_after_opening_tag' => true, 18 | 'no_useless_else' => true, 19 | 'no_useless_return' => true, 20 | 'ordered_imports' => true, 21 | 'php_unit_strict' => true, 22 | 'phpdoc_annotation_without_dot' => false, 23 | 'phpdoc_order' => true, 24 | 'semicolon_after_instruction' => true, 25 | 'strict_comparison' => true, 26 | 'strict_param' => true, 27 | 'self_accessor' => false, 28 | ]) 29 | ->setFinder($finder) 30 | ; 31 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.0 5 | - 7.1 6 | - 7.2 7 | - 7.3 8 | - 7.4 9 | 10 | sudo: false 11 | 12 | before_script: 13 | - travis_retry composer self-update 14 | - travis_retry composer update --no-interaction --no-suggest --prefer-dist 15 | 16 | script: 17 | - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover 18 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## [v0.2.0](https://github.com/stefanbauer/laravel-favicon-extractor/tree/v0.2.0) (2018-08-21) 4 | [Full Changelog](https://github.com/stefanbauer/laravel-favicon-extractor/compare/v0.1.0...v0.2.0) 5 | 6 | ## [v0.1.0](https://github.com/stefanbauer/laravel-favicon-extractor/tree/v0.1.0) (2018-08-20) 7 | 8 | 9 | \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | If you want to contribute to one of my projects and make it better, **your help is very welcome**. It's also a great way to learn more about **social coding on Github** and **new technologies**. 4 | 5 | For contribution please create a PR on [Github](https://github.com/stefanbauer/laravel-favicon-extractor). 6 | 7 | ## Pull Requests 8 | 9 | #### Coding Standards 10 | 11 | I use the **[Symfony Coding Standard](https://symfony.com/doc/current/contributing/code/standards.html)**. It's basically the **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)**, just with some additional standards. I always add a `.php_cs.dist`. Run the `php-cs-fixer` to keep code valid. 12 | 13 | #### Feature Branches 14 | 15 | - **Use feature branches:** Create a new branch to work on. Branch from `master`. 16 | - **One feature per PR:** I love simplicity. Please create only one PR per feature. If you would like to change more, create more PRs. 17 | 18 | #### Commits 19 | 20 | - **Don't pollute the git history:** Please don't create several (senseless) commits. Squash/Rebase your commits into one or several meaningful commits. 21 | - **Commit in present tense:** Always write your commit messages in the present tense. Your commit message should describe what the commit, when applied, does to the code – not what you did to the code. 22 | 23 | #### Tests 24 | 25 | - **Don't forget your tests:** I don't accept your PRs without any tests. 26 | 27 | #### Documentation 28 | - **Document behaviour related things:** Update the `README.md` with details of changes if necessary. 29 | 30 | ## Running Tests 31 | 32 | ``` bash 33 | $ vendor/bin/phpunit 34 | ``` 35 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Stefan Bauer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 19 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 21 | OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Favicon Extractor 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/stefanbauer/laravel-favicon-extractor.svg?style=flat-square)](https://packagist.org/packages/stefanbauer/laravel-favicon-extractor) 4 | [![Build Status](https://img.shields.io/travis/stefanbauer/laravel-favicon-extractor/master.svg?style=flat-square)](https://travis-ci.org/stefanbauer/laravel-favicon-extractor) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/stefanbauer/laravel-favicon-extractor.svg?style=flat-square)](https://packagist.org/packages/stefanbauer/laravel-favicon-extractor) 6 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 7 | 8 | This package provides a convenient way to extract a favicon from any website by using the appropriate Google service. It allows you to fetch and save it to your local storage. 9 | 10 | ## Usage 11 | 12 | Usage is very simple. You can either pull it in via Dependency Injection or use the Facade. 13 | 14 | - For the Dependency Injection version, type hint the `FaviconExtractorInterface`. 15 | - For the Facade version, use the `FaviconExtractor` Facade. 16 | 17 | ### General 18 | 19 | - If no favicon could be found, it returns a default one. 20 | - The favicon's extension is always `.png`. It's not necessary to be part of your filename. 21 | 22 | ### Fetch the favicon only 23 | 24 | ```php 25 | $favicon = FaviconExtractor::fromUrl('https://laravel.com')->fetchOnly(); 26 | ``` 27 | 28 | It returns a instance which implements `FaviconInterface` where you can retrieve the raw content of the favicon with `$favicon->getContent()`. 29 | 30 | ### Fetch and download the favicon 31 | 32 | If you prefer to save the favicon to your local storage, you can. The only requirement is to define the path, where the favicon should be saved. It's relative to your root path which you defined in `config/filesystems.php`. Saying your path to save is `favicons`, it will be saved to `app/storage/favicons`. 33 | 34 | #### With a random generated filename 35 | 36 | ```php 37 | FaviconExtractor::fromUrl('https://laravel.com')->fetchAndSaveTo('favicons'); 38 | // returns favicons/HIgLtwL0iUdNkwfq.png 39 | ``` 40 | 41 | #### With a custom filename 42 | 43 | ```php 44 | FaviconExtractor::fromUrl('https://laravel.com')->fetchAndSaveTo('favicons', 'myFilename'); 45 | // returns favicons/myFilename.png 46 | ``` 47 | 48 | ## Installation 49 | 50 | To install this package, require it via composer. 51 | 52 | ```shell 53 | $ composer require stefanbauer/laravel-favicon-extractor 54 | ``` 55 | 56 | Thanks to Laravel 5.5+ Package Auto-Discovery, there is no need to add the ServiceProvider manually. If you don't use auto-discovery, add the ServiceProvider to the providers array in `config/app.php`. 57 | 58 | ```php 59 | StefanBauer\LaravelFaviconExtractor\FaviconExtractorServiceProvider::class, 60 | ``` 61 | 62 | If you want to use the facade, add this to your facades array in `config/app.php`. 63 | 64 | ```php 65 | 'FaviconExtractor' => StefanBauer\LaravelFaviconExtractor\Facades\FaviconExtractor::class, 66 | ``` 67 | 68 | ## Configuration 69 | 70 | If you would like to modify the configuration, use the publish command to copy the package config over. 71 | 72 | ```shell 73 | php artisan vendor:publish --provider="StefanBauer\LaravelFaviconExtractor\FaviconExtractorServiceProvider" --tag="config" 74 | ``` 75 | 76 | The configuration file has only two options you can change. The `provider_class` and the `filename_generator_class`. In general, there is no need to change it, unless you like to have a different implementations how the favicon is fetched and how the filename is generated. Pleae take care of implementing the corresponding interfaces. 77 | 78 | ## Testing 79 | 80 | ```shell 81 | $ vendor/bin/phpunit 82 | ``` 83 | 84 | ## Changelog 85 | 86 | Please take a look at the [CHANGELOG](CHANGELOG.md) what has changed recently. 87 | 88 | ## Contributing 89 | 90 | Please take a look at [CONTRIBUTING](CONTRIBUTING.md) for more information. 91 | 92 | ## License 93 | 94 | The MIT License (MIT). Please take a look at the [LICENSE](LICENSE.md) for more information. 95 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stefanbauer/laravel-favicon-extractor", 3 | "description": "A favicon extractor for Laravel", 4 | "keywords": [ 5 | "stefanbauer", 6 | "laravel-favicon-extractor", 7 | "favicon" 8 | ], 9 | "homepage": "https://github.com/stefanbauer/laravel-favicon-extractor", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Stefan Bauer", 14 | "email": "mail@stefanbauer.me", 15 | "homepage": "https://stefanbauer.me" 16 | } 17 | ], 18 | "require": { 19 | "php" : "^7.0|^8.0", 20 | "illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0", 21 | "graham-campbell/guzzle-factory": "^3.0|^4.0" 22 | }, 23 | "require-dev": { 24 | "phpunit/phpunit": "^6.2|^7.0|^8.0|^9.0", 25 | "orchestra/testbench": "~3.5.0|~3.6.0", 26 | "mockery/mockery": "^1.1", 27 | "friendsofphp/php-cs-fixer": "^2.12" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "StefanBauer\\LaravelFaviconExtractor\\": "src" 32 | } 33 | }, 34 | "autoload-dev": { 35 | "psr-4": { 36 | "StefanBauer\\LaravelFaviconExtractor\\": "tests" 37 | } 38 | }, 39 | "extra": { 40 | "laravel": { 41 | "providers": [ 42 | "StefanBauer\\LaravelFaviconExtractor\\FaviconExtractorServiceProvider" 43 | ], 44 | "aliases": { 45 | "FaviconExtractor": "StefanBauer\\LaravelFaviconExtractor\\Facades\\FaviconExtractor" 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /config/favicon-extractor.php: -------------------------------------------------------------------------------- 1 | \StefanBauer\LaravelFaviconExtractor\Provider\GoogleProvider::class, 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Filename Generator 21 | |-------------------------------------------------------------------------- 22 | | 23 | | If you don't specify a custom filename on saving the downloaded favicon 24 | | to your storage, this package generates a random string for it. You 25 | | can override this behaviour at any time by using a custom 26 | | implementation which implements the following interface. 27 | | 28 | | \StefanBauer\LaravelFaviconExtractor\Generator\FilenameGeneratorInterface 29 | | 30 | */ 31 | 'filename_generator_class' => \StefanBauer\LaravelFaviconExtractor\Generator\FilenameGenerator::class, 32 | ]; 33 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | ./tests/ 19 | 20 | 21 | 22 | 23 | 24 | ./src 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/Exception/FaviconCouldNotBeSavedException.php: -------------------------------------------------------------------------------- 1 | content = $content; 14 | } 15 | 16 | public function getContent(): string 17 | { 18 | return $this->content; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Favicon/FaviconFactory.php: -------------------------------------------------------------------------------- 1 | provider = $provider; 27 | $this->faviconFactory = $faviconFactory; 28 | $this->filenameGenerator = $filenameGenerator; 29 | } 30 | 31 | public function fromUrl(string $url): FaviconExtractorInterface 32 | { 33 | $this->url = $url; 34 | 35 | return $this; 36 | } 37 | 38 | public function getUrl(): string 39 | { 40 | return $this->url; 41 | } 42 | 43 | public function fetchOnly(): FaviconInterface 44 | { 45 | $this->favicon = $this->faviconFactory->create( 46 | $this->provider->fetchFromUrl($this->getUrl()) 47 | ); 48 | 49 | return $this->favicon; 50 | } 51 | 52 | public function fetchAndSaveTo(string $path, string $filename = null): string 53 | { 54 | if (null === $filename) { 55 | $filename = $this->filenameGenerator->generate(16); 56 | } 57 | 58 | $favicon = $this->fetchOnly(); 59 | $targetPath = $this->getTargetPath($path, $filename); 60 | 61 | if (!Storage::put($targetPath, $favicon->getContent())) { 62 | throw new FaviconCouldNotBeSavedException(sprintf( 63 | 'The favicon of %s could not be saved at path "%s" ', 64 | $this->getUrl(), $targetPath 65 | )); 66 | } 67 | 68 | return Str::replaceFirst('public/', '', $targetPath); 69 | } 70 | 71 | private function getTargetPath(string $path, string $filename): string 72 | { 73 | return $path.DIRECTORY_SEPARATOR.$filename.'.png'; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/FaviconExtractorInterface.php: -------------------------------------------------------------------------------- 1 | publishes([ 18 | __DIR__.'/../config/favicon-extractor.php' => config_path('favicon-extractor.php') 19 | ], 'config'); 20 | 21 | $this->app->bind( 22 | FaviconFactoryInterface::class, 23 | FaviconFactory::class 24 | ); 25 | 26 | $this->app->bind( 27 | ProviderInterface::class, 28 | config('favicon-extractor.provider_class') 29 | ); 30 | 31 | $this->app->bind( 32 | FilenameGeneratorInterface::class, 33 | config('favicon-extractor.filename_generator_class') 34 | ); 35 | 36 | $this->app->bind( 37 | FaviconExtractorInterface::class, 38 | FaviconExtractor::class 39 | ); 40 | 41 | $this->app->alias(FaviconExtractorInterface::class, 'favicon.extractor'); 42 | } 43 | 44 | public function register() 45 | { 46 | $this->mergeConfigFrom(__DIR__.'/../config/favicon-extractor.php', 'favicon-extractor'); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Generator/FilenameGenerator.php: -------------------------------------------------------------------------------- 1 | get($this->getUrl($url)); 16 | 17 | return $response->getBody()->getContents(); 18 | } 19 | 20 | private function getUrl(string $url): string 21 | { 22 | $domain = preg_replace('/https?:\/\/([\w\-\.]+)\/?.*/i', '$1', $url); 23 | 24 | return 'https://icons.duckduckgo.com/ip3/'.$domain; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Provider/GitHubProvider.php: -------------------------------------------------------------------------------- 1 | get($this->getUrl($url)); 15 | 16 | return $response->getBody()->getContents(); 17 | } 18 | 19 | private function getUrl(string $url): string 20 | { 21 | $domain = preg_replace('/https?:\/\/(www\.)?([\w\-\.]+)\/?.*/i', '$2', $url); 22 | 23 | return 'https://favicons.githubusercontent.com/'.urlencode($domain); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Provider/GoogleProvider.php: -------------------------------------------------------------------------------- 1 | get($this->getUrl($url)); 15 | 16 | return $response->getBody()->getContents(); 17 | } 18 | 19 | private function getUrl(string $url): string 20 | { 21 | return 'https://www.google.com/s2/favicons?domain='.urlencode($url); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Provider/ProviderInterface.php: -------------------------------------------------------------------------------- 1 | create($expectedContent = 'content'); 15 | 16 | $this->assertInstanceOf(Favicon::class, $favicon); 17 | $this->assertSame($expectedContent, $favicon->getContent()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Favicon/FaviconTest.php: -------------------------------------------------------------------------------- 1 | assertSame($expectedContent, $favicon->getContent()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/FaviconExtractorTest.php: -------------------------------------------------------------------------------- 1 | faviconFactory = \Mockery::mock(FaviconFactoryInterface::class); 42 | $this->provider = \Mockery::mock(ProviderInterface::class); 43 | $this->filenameGenerator = \Mockery::mock(FilenameGeneratorInterface::class); 44 | 45 | $this->extractor = new FaviconExtractor($this->faviconFactory, $this->provider, $this->filenameGenerator); 46 | 47 | parent::setUp(); 48 | } 49 | 50 | public function test_it_fetches_the_favicon() 51 | { 52 | $expectedUrl = 'http://example.com'; 53 | $expectedContent = 'example-content'; 54 | 55 | $this->provider 56 | ->shouldReceive('fetchFromUrl') 57 | ->once() 58 | ->with($expectedUrl) 59 | ->andReturn($expectedContent) 60 | ; 61 | 62 | $this->faviconFactory 63 | ->shouldReceive('create') 64 | ->once() 65 | ->with($expectedContent) 66 | ; 67 | 68 | $this->extractor->fromUrl($expectedUrl)->fetchOnly(); 69 | } 70 | 71 | public function test_it_generates_a_filename_if_none_given() 72 | { 73 | $this->provider->shouldIgnoreMissing(); 74 | 75 | $expectedFavicon = new Favicon('content'); 76 | 77 | $this->faviconFactory 78 | ->shouldReceive('create') 79 | ->withAnyArgs() 80 | ->andReturn($expectedFavicon) 81 | ; 82 | 83 | $this->filenameGenerator 84 | ->shouldReceive('generate') 85 | ->once() 86 | ->with(16) 87 | ->andReturn('random-filename') 88 | ; 89 | 90 | $this->extractor 91 | ->fromUrl('http://example.com') 92 | ->fetchAndSaveTo('some-path') 93 | ; 94 | } 95 | 96 | public function test_it_saves_it_properly() 97 | { 98 | $this->provider->shouldIgnoreMissing(); 99 | 100 | $expectedFavicon = new Favicon('content'); 101 | 102 | $this->faviconFactory 103 | ->shouldReceive('create') 104 | ->withAnyArgs() 105 | ->andReturn($expectedFavicon) 106 | ; 107 | 108 | Storage::fake(); 109 | Storage:: 110 | shouldReceive('put') 111 | ->once() 112 | ->with('some-path/a-filename.png', 'content') 113 | ->andReturn(true) 114 | ; 115 | 116 | $this->extractor 117 | ->fromUrl('http://example.com') 118 | ->fetchAndSaveTo('some-path', 'a-filename') 119 | ; 120 | } 121 | 122 | public function test_it_throws_an_exception_when_saving_was_not_successful() 123 | { 124 | $this->provider->shouldIgnoreMissing(); 125 | 126 | $expectedFavicon = new Favicon('content'); 127 | 128 | $this->faviconFactory 129 | ->shouldReceive('create') 130 | ->withAnyArgs() 131 | ->andReturn($expectedFavicon) 132 | ; 133 | 134 | Storage::fake(); 135 | Storage:: 136 | shouldReceive('put') 137 | ->once() 138 | ->andReturn(false) 139 | ; 140 | 141 | $this->expectException(FaviconCouldNotBeSavedException::class); 142 | 143 | $this->extractor 144 | ->fromUrl('http://example.com') 145 | ->fetchAndSaveTo('some-path', 'a-filename') 146 | ; 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /tests/Generator/FilenameGeneratorTest.php: -------------------------------------------------------------------------------- 1 | generate($expectedLength = 10); 15 | 16 | $this->assertSame($expectedLength, strlen($generated)); 17 | } 18 | } 19 | --------------------------------------------------------------------------------