Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbauer committed Aug 20, 2018
0 parents commit 064c24a
Show file tree
Hide file tree
Showing 28 changed files with 838 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
30 changes: 30 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace PhpCsFixer;

$finder = Finder::create()
->name('*.php')
->in(__DIR__.DIRECTORY_SEPARATOR.'src')
->in(__DIR__.DIRECTORY_SEPARATOR.'tests')
;

return Config::create()
->setUsingCache(false)
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
'linebreak_after_opening_tag' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'php_unit_strict' => true,
'phpdoc_annotation_without_dot' => false,
'phpdoc_order' => true,
'semicolon_after_instruction' => true,
'strict_comparison' => true,
'strict_param' => true,
'self_accessor' => false,
])
->setFinder($finder)
;
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: php

php:
- 7.0
- 7.1
- 7.2

sudo: false

before_script:
- travis_retry composer self-update
- travis_retry composer update --no-interaction --no-suggest --prefer-dist

script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Change Log



\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
34 changes: 34 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Contributing

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**.

For contribution please create a PR on [Github](https://github.com/stefanbauer/laravel-favicon-extractor).

## Pull Requests

#### Coding Standards

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.

#### Feature Branches

- **Use feature branches:** Create a new branch to work on. Branch from `master`.
- **One feature per PR:** I love simplicity. Please create only one PR per feature. If you would like to change more, create more PRs.

#### Commits

- **Don't pollute the git history:** Please don't create several (senseless) commits. Squash/Rebase your commits into one or several meaningful commits.
- **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.

#### Tests

- **Don't forget your tests:** I don't accept your PRs without any tests.

#### Documentation
- **Document behaviour related things:** Update the `README.md` with details of changes if necessary.

## Running Tests

``` bash
$ vendor/bin/phpunit
```
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Stefan Bauer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Laravel Favicon Extractor

[![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)
[![Build Status](https://img.shields.io/travis/stefanbauer/laravel-favicon-extractor/master.svg?style=flat-square)](https://travis-ci.org/stefanbauer/laravel-favicon-extractor)
[![Total Downloads](https://img.shields.io/packagist/dt/stefanbauer/laravel-favicon-extractor.svg?style=flat-square)](https://packagist.org/packages/stefanbauer/laravel-favicon-extractor)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)

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.

## Usage

Usage is very simple. You can either pull it in via Dependency Injection or use the Facade.

- For the Dependency Injection version, type hint the `FaviconExtractorInterface`.
- For the Facade version, use the `FaviconExtractor` Facade.

### General

- If no favicon could be found, it returns a default one.
- The favicon's extension is always `.png`. It's not necessary to be part of your filename.

### Fetch the favicon only

```php
$favicon = FaviconExtractor::fromUrl('https://laravel.com')->fetchOnly();
```

It returns a instance which implements `FaviconInterface` where you can retrieve the raw content of the favicon with `$favicon->getContent()`.

### Fetch and download the favicon

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`.

#### With a random generated filename

```php
FaviconExtractor::fromUrl('https://laravel.com')->fetchAndSaveTo('favicons');
// returns favicons/HIgLtwL0iUdNkwfq.png
```

#### With a custom filename

```php
FaviconExtractor::fromUrl('https://laravel.com')->fetchAndSaveTo('favicons', 'myFilename');
// returns favicons/myFilename.png
```

## Installation

To install this package, require it via composer.

```shell
$ composer require stefanbauer/laravel-favicon-extractor
```

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`.

```php
StefanBauer\LaravelFaviconExtractor\FaviconExtractorServiceProvider::class,
```

If you want to use the facade, add this to your facades array in `config/app.php`.

```php
'FaviconExtractor' => StefanBauer\LaravelFaviconExtractor\Facades\FaviconExtractor::class,
```

## Configuration

If you would like to modify the configuration, use the publish command to copy the package config over.

```shell
php artisan vendor:publish --provider="StefanBauer\LaravelFaviconExtractor\FaviconExtractorServiceProvider" --tag="config"
```

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.

## Testing

```shell
$ vendor/bin/phpunit
```

## Changelog

Please take a look at the [CHANGELOG](CHANGELOG.md) what has changed recently.

## Contributing

Please take a look at [CONTRIBUTING](CONTRIBUTING.md) for more information.

## License

The MIT License (MIT). Please take a look at the [LICENSE](LICENSE.md) for more information.
49 changes: 49 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "stefanbauer/laravel-favicon-extractor",
"description": "A favicon extractor for Laravel",
"keywords": [
"stefanbauer",
"laravel-favicon-extractor",
"favicon"
],
"homepage": "https://github.com/stefanbauer/laravel-favicon-extractor",
"license": "MIT",
"authors": [
{
"name": "Stefan Bauer",
"email": "[email protected]",
"homepage": "https://stefanbauer.me"
}
],
"require": {
"php" : "^7.0",
"illuminate/support": "~5.5.0|~5.6.0",
"graham-campbell/guzzle-factory": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^6.2|^7.0",
"orchestra/testbench": "~3.5.0|~3.6.0",
"mockery/mockery": "^1.1",
"friendsofphp/php-cs-fixer": "^2.12"
},
"autoload": {
"psr-4": {
"StefanBauer\\LaravelFaviconExtractor\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"StefanBauer\\LaravelFaviconExtractor\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"StefanBauer\\LaravelFaviconExtractor\\FaviconExtractorServiceProvider"
],
"aliases": {
"FaviconExtractor": "StefanBauer\\LaravelFaviconExtractor\\Facades\\FaviconExtractor"
}
}
}
}
32 changes: 32 additions & 0 deletions config/favicon-extractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Favicon Provider
|--------------------------------------------------------------------------
|
| This class is used for fetching favicons. You can swap it out easily if
| you like as long as you implement the ProviderInterface.
|
| \StefanBauer\LaravelFaviconExtractor\Provider\ProviderInterface
|
*/

'provider_class' => \StefanBauer\LaravelFaviconExtractor\Provider\GoogleProvider::class,

/*
|--------------------------------------------------------------------------
| Filename Generator
|--------------------------------------------------------------------------
|
| If you don't specify a custom filename on saving the downloaded favicon
| to your storage, this package generates a random string for it. You
| can override this behaviour at any time by using a custom
| implementation which implements the following interface.
|
| \StefanBauer\LaravelFaviconExtractor\Generator\FilenameGeneratorInterface
|
*/
'filename_generator_class' => \StefanBauer\LaravelFaviconExtractor\Generator\FilenameGenerator::class
];
27 changes: 27 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<php>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
9 changes: 9 additions & 0 deletions src/Exception/FaviconCouldNotBeSavedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace StefanBauer\LaravelFaviconExtractor\Exception;

class FaviconCouldNotBeSavedException extends \RuntimeException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidUrlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace StefanBauer\LaravelFaviconExtractor\Exception;

class InvalidUrlException extends \RuntimeException
{
}
15 changes: 15 additions & 0 deletions src/Facades/FaviconExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace StefanBauer\LaravelFaviconExtractor\Facades;

use Illuminate\Support\Facades\Facade;

class FaviconExtractor extends Facade
{
protected static function getFacadeAccessor()
{
return 'favicon.extractor';
}
}
20 changes: 20 additions & 0 deletions src/Favicon/Favicon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace StefanBauer\LaravelFaviconExtractor\Favicon;

class Favicon implements FaviconInterface
{
private $content;

public function __construct($content)
{
$this->content = $content;
}

public function getContent(): string
{
return $this->content;
}
}
13 changes: 13 additions & 0 deletions src/Favicon/FaviconFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace StefanBauer\LaravelFaviconExtractor\Favicon;

class FaviconFactory implements FaviconFactoryInterface
{
public function create(string $content): Favicon
{
return new Favicon($content);
}
}
Loading

0 comments on commit 064c24a

Please sign in to comment.