Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Port heise shariff code #29

Merged
merged 3 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,27 @@ return [
];
```

Define cache, http client and request factory:

```
# config/routes/core23_shariff.yaml

framework:
cache:
pools:
cache.shariff:
adapter: cache.adapter.filesystem

core23_shariff:
cache: 'cache.shariff'
http_client: 'some.http.client' # e.g httplug.client
request_factory: 'some.request.factory' # e.g. nyholm.psr7.psr17_factory
```

### Assets

It is recommended to use [webpack](https://webpack.js.org/) / [webpack-encore](https://github.com/symfony/webpack-encore)
to include the `shariff.js` and `shariff.css` file in your page.
It is recommended to use [webpack](https://webpack.js.org/) / [webpack-encore](https://github.com/symfony/webpack-encore)
to include the `shariff.js` and `shariff.css` file in your page.

You can use [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/) to load the library:

Expand All @@ -62,14 +79,15 @@ core23_shariff:
resource: "@Core23ShariffBundle/Resources/config/routing/backend.yml"
```

Create a configuration file called `doctrine_cache.yaml`:
Create a configuration file called `framework_cache.yaml`:

```yaml
doctrine_cache:
providers:
core23_shariff:
type: php_file
namespace: core23_shariff
framework:
cache:
pools:
cache.shariff:
adapter: cache.adapter.filesystem

```

## Usage
Expand All @@ -84,21 +102,26 @@ doctrine_cache:

### Configure the Bundle

You can globally configure the services that should count the likes or favorites for a page.
You can globally configure the services that should count the likes or favorites for a page.

```yaml
core23_shariff:
cache: 'cache.shariff'
http_client: 'some.http.client'
request_factory: 'some.request.factory'

options:
domains: [ ] # Allow specific domains for shariff
services: [ 'GooglePlus', 'Facebook', 'LinkedIn', 'Reddit', 'StumbleUpon', 'Flattr', 'Pinterest', 'Xing', 'AddThis' ]
services: [ 'addthis', 'buffer', 'facebook', 'pinterest', 'reddit', 'stumbleupon', 'vk', 'xing' ]

services:
# Optional configuration when using facebook service
facebook:
app_id: "1234567890"
secret: "GENERATEDSECRET"
app_id: "1234567890"
secret: "GENERATEDSECRET"
version: "5.0"
```

See [shariff-php] for a list of all available services.
This is a fork of [shariff-php] with a more modern and dynamic solution.

## License

Expand Down
File renamed without changes.
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"require": {
"php": "^7.2",
"ext-json": "*",
"doctrine/cache": "^1.8",
"doctrine/doctrine-cache-bundle": "^1.3.5",
"guzzlehttp/guzzle": "^6.3.3",
"heise/shariff": "^8.2",
"psr/cache": "^1.0",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0",
"sonata-project/block-bundle": "^3.18",
"sonata-project/core-bundle": "^3.17",
"symfony/config": "^3.4 || ^4.2",
Expand All @@ -43,7 +43,10 @@
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.3",
"sllh/composer-lint": "^1.0"
"nyholm/psr7": "^1.0",
"sllh/composer-lint": "^1.0",
"symfony/cache": "^3.4 || ^4.2",
"symfony/http-client": "^3.4 || ^4.2"
},
"config": {
"sort-packages": true
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"homepage": "https://core23.de",
"author": "Christian Gripp <mail@core23.de>",
"dependencies": {
"shariff": "^3.0"
"shariff": "^3.2"
},
"main": "assets/npm.js",
"main": "assets/modules.js",
"files": [
"assets/"
]
Expand Down
21 changes: 20 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,23 @@ parameters:
ignoreErrors:
# Symfony DI
- '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::children\(\).#'
- "/Call to function method_exists.. with 'Symfony.+' and 'getRootNode' will always evaluate to false./"

-
message: "#^Call to function in_array\\(\\) with arguments string, array\\(\\) and true will always evaluate to false\\.$#"
count: 1
path: src/Backend/PsrBackend.php

-
message: "#^Offset 'host' does not exist on array\\(\\)\\|array\\('scheme' \\=\\> string, 'host' \\=\\> string, 'port' \\=\\> int, 'user' \\=\\> string, 'pass' \\=\\> string, 'path' \\=\\> string, 'query' \\=\\> string, 'fragment' \\=\\> string\\)\\.$#"
count: 1
path: src/Backend/PsrBackend.php

-
message: "#^Call to function method_exists\\(\\) with 'Symfony\\\\\\\\Component…' and 'getRootNode' will always evaluate to false\\.$#"
count: 1
path: src/DependencyInjection/Configuration.php

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\TreeBuilder\\:\\:root\\(\\)\\.$#"
count: 1
path: src/DependencyInjection/Configuration.php
6 changes: 3 additions & 3 deletions src/Action/BackendAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@

namespace Core23\ShariffBundle\Action;

use Core23\ShariffBundle\Backend\BackendManager;
use Core23\ShariffBundle\Backend\Backend;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

final class BackendAction
{
/**
* @var BackendManager
* @var Backend
*/
private $backend;

public function __construct(BackendManager $backend)
public function __construct(Backend $backend)
{
$this->backend = $backend;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Backend/Backend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\ShariffBundle\Backend;

interface Backend
{
/**
* @return array<string, int>
*/
public function get(string $url): array;
}
32 changes: 0 additions & 32 deletions src/Backend/BackendManager.php

This file was deleted.

147 changes: 147 additions & 0 deletions src/Backend/PsrBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\ShariffBundle\Backend;

use Core23\ShariffBundle\Manager\ServiceManager;
use Core23\ShariffBundle\Service\Exception\FetchException;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;

final class PsrBackend implements Backend, LoggerAwareInterface
{
use LoggerAwareTrait;

/**
* @var ServiceManager
*/
private $serviceManager;

/**
* @var RequestFactoryInterface
*/
private $requestFactory;

/**
* @var ClientInterface
*/
private $client;

/**
* @var CacheItemPoolInterface
*/
private $cache;

/**
* @var string[]
*/
private $domains;

/**
* @param string[] $domains
*/
public function __construct(
ServiceManager $serviceManager,
RequestFactoryInterface $requestFactory,
ClientInterface $client,
CacheItemPoolInterface $cache,
array $domains = []
) {
$this->serviceManager = $serviceManager;
$this->requestFactory = $requestFactory;
$this->client = $client;
$this->cache = $cache;
$this->domains = $domains;
$this->logger = new NullLogger();
}

public function get(string $url): array
{
if (!$this->isValidDomain($url)) {
return [];
}

$cacheKey = $this->cacheKey($url);

if ($this->cache->hasItem($cacheKey)) {
return json_decode($this->cache->getItem($cacheKey)->get(), true);
}

if (false === filter_var($url, FILTER_VALIDATE_URL)) {
return [];
}

$counts = $this->fetchServiceCounters($url);

$this->saveCacheEntry($cacheKey, $counts);

return $counts;
}

private function isValidDomain(string $url): bool
{
if (0 === \count($this->domains)) {
$parsed = parse_url($url);

if (false === $parsed) {
return false;
}

return \in_array($parsed['host'], $this->domains, true);
}

return true;
}

private function cacheKey(string $url): string
{
return md5($url);
}

private function fetchServiceCounters(string $url): array
{
$counts = [];

foreach ($this->serviceManager->getActive() as $service) {
$request = $service->createRequest($this->requestFactory, $url);

try {
$response = $this->client->sendRequest($request);

$counts[$service->getCode()] = $service->count($response);
} catch (FetchException $e) {
$this->logger->warning($e->getMessage(), [
'exception' => $e,
'responseBody' => $e->getResponseBody(),
]);
} catch (ClientExceptionInterface $e) {
$this->logger->warning($e->getMessage(), [
'exception' => $e,
]);
}
}

return $counts;
}

private function saveCacheEntry(string $cacheKey, array $counts): void
{
$item = $this->cache->getItem($cacheKey);
$item->set(json_encode($counts));

$this->cache->save($item);
}
}
Loading