Skip to content

Commit

Permalink
[TASK] Remove zend-json dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
liayn committed Aug 2, 2016
1 parent c6155ec commit 6898d23
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 170 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Requirements
To run Shariff PHP Backend on your server you need one of these PHP versions:

* 5.6
* 7.0
* 7.0-7.1

Older versions and HHVM are not supported.

Expand All @@ -37,7 +37,9 @@ Installing the Shariff backend on you own server

To run Shariff under a certain URL, unzip the [release](https://github.com/heiseonline/shariff-backend-php/releases) zip file into a directory under the document root of your web server.

This zip file contains a configuration file `shariff.json`. The following configuration options are available:
Take a look into the demo application in the `index.php` file and adjust the configuration as necessary.

The following configuration options are available:

| Key | Type | Description |
|-------------|------|-------------|
Expand Down
1 change: 0 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mkdir -p build
rm -fr build/*
composer install --prefer-dist --no-dev
cp index.php build
cp shariff.json build
cp -a vendor build
cp -a src build
composer install
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.1",
"psr/log": "1.0.0",
"zendframework/zend-cache": "*",
"zendframework/zend-json": "*",
"zendframework/zend-config": "*"
"zendframework/zend-cache": "^2.7"
},
"require-dev": {
"phpunit/phpunit": "~5.3",
Expand Down
130 changes: 12 additions & 118 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 34 additions & 8 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,49 @@
require_once __DIR__.'/vendor/autoload.php';

use Heise\Shariff\Backend;
use Zend\Config\Reader\Json;

/**
* Demo Application using Shariff Backend
*/
class Application
{
/**
* Sample configuration
*
* @var array
*/
private static $configuration = [
'cache' => [
'ttl' => 60
],
'domains' => [
'www.heise.de',
'www.ct.de'
],
'services' => [
'GooglePlus',
'Facebook',
'LinkedIn',
'Reddit',
'StumbleUpon',
'Flattr',
'Pinterest',
'Xing',
'AddThis'
]
];

public static function run()
{
header('Content-type: application/json');

if (!isset($_GET["url"])) {
$url = isset($_GET['url']) ? $_GET['url'] : '';
if ($url) {
$shariff = new Backend(self::$configuration);
echo json_encode($shariff->get($url));
} else {
echo json_encode(null);
return;
}

$reader = new Json();

$shariff = new Backend($reader->fromFile('shariff.json'));
echo json_encode($shariff->get($_GET["url"]));
}
}

Expand Down
20 changes: 0 additions & 20 deletions shariff.json

This file was deleted.

16 changes: 8 additions & 8 deletions src/Backend/BackendManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ public function setLogger(LoggerInterface $logger = null)
public function get($url)
{

// Aenderungen an der Konfiguration invalidieren den Cache
$cache_key = md5($url.$this->baseCacheKey);
// Changing configuration invalidates the cache
$cacheKey = md5($url.$this->baseCacheKey);

if (!filter_var($url, FILTER_VALIDATE_URL)) {
return;
return null;
}

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

if (!$this->isValidDomain($url)) {
return;
return null;
}

$requests = array_map(
Expand All @@ -116,7 +116,7 @@ function ($service) use ($url) {
$this->services
);

/** @var ResponseInterface[] $results */
/** @var ResponseInterface[]|TransferException[] $results */
$results = Pool::batch($this->client, $requests);

$counts = [];
Expand All @@ -139,7 +139,7 @@ function ($service) use ($url) {
}
}

$this->cache->setItem($cache_key, json_encode($counts));
$this->cache->setItem($cacheKey, json_encode($counts));

return $counts;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Backend/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ protected function getAccessToken()
return $response->getBody();
}

return;
return null;
}
}
10 changes: 1 addition & 9 deletions src/ZendCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,7 @@ public function __construct(array $configuration)
}

if ($cache instanceof ClearExpiredInterface) {
if (function_exists('register_postsend_function')) {
// for hhvm installations: executing after response / session close
register_postsend_function(function () use ($cache) {
$cache->clearExpired();
});
} else {
// default
$cache->clearExpired();
}
$cache->clearExpired();
}

$this->cache = $cache;
Expand Down

0 comments on commit 6898d23

Please sign in to comment.