diff --git a/README.md b/README.md index e142666..7927ddd 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 | |-------------|------|-------------| diff --git a/build.sh b/build.sh index 846a16b..a039829 100755 --- a/build.sh +++ b/build.sh @@ -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 diff --git a/composer.json b/composer.json index 4d5568f..3704899 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index aed6e49..14f3e49 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "dc34bba1a6a70b365025c80285bc790e", - "content-hash": "eab074ad95c0c2215c10c45cbb9a4914", + "hash": "b09974b79419a4e346100c9697e9d571", + "content-hash": "257d4bfcc8ded65217e75c460d0f84ed", "packages": [ { "name": "container-interop/container-interop", @@ -361,62 +361,6 @@ ], "time": "2016-05-12 21:47:55" }, - { - "name": "zendframework/zend-config", - "version": "2.6.0", - "source": { - "type": "git", - "url": "https://github.com/zendframework/zend-config.git", - "reference": "2920e877a9f6dca9fa8f6bd3b1ffc2e19bb1e30d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/zendframework/zend-config/zipball/2920e877a9f6dca9fa8f6bd3b1ffc2e19bb1e30d", - "reference": "2920e877a9f6dca9fa8f6bd3b1ffc2e19bb1e30d", - "shasum": "" - }, - "require": { - "php": "^5.5 || ^7.0", - "zendframework/zend-stdlib": "^2.7 || ^3.0" - }, - "require-dev": { - "fabpot/php-cs-fixer": "1.7.*", - "phpunit/phpunit": "~4.0", - "zendframework/zend-filter": "^2.6", - "zendframework/zend-i18n": "^2.5", - "zendframework/zend-json": "^2.6.1", - "zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3" - }, - "suggest": { - "zendframework/zend-filter": "Zend\\Filter component", - "zendframework/zend-i18n": "Zend\\I18n component", - "zendframework/zend-json": "Zend\\Json to use the Json reader or writer classes", - "zendframework/zend-servicemanager": "Zend\\ServiceManager for use with the Config Factory to retrieve reader and writer instances" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.6-dev", - "dev-develop": "2.7-dev" - } - }, - "autoload": { - "psr-4": { - "Zend\\Config\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "description": "provides a nested object property based user interface for accessing this configuration data within application code", - "homepage": "https://github.com/zendframework/zend-config", - "keywords": [ - "config", - "zf2" - ], - "time": "2016-02-04 23:01:10" - }, { "name": "zendframework/zend-eventmanager", "version": "3.0.1", @@ -471,56 +415,6 @@ ], "time": "2016-02-18 20:53:00" }, - { - "name": "zendframework/zend-json", - "version": "3.0.0", - "source": { - "type": "git", - "url": "https://github.com/zendframework/zend-json.git", - "reference": "f42a1588e75c2a3e338cd94c37906231e616daab" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/zendframework/zend-json/zipball/f42a1588e75c2a3e338cd94c37906231e616daab", - "reference": "f42a1588e75c2a3e338cd94c37906231e616daab", - "shasum": "" - }, - "require": { - "php": "^5.5 || ^7.0" - }, - "require-dev": { - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "^2.3", - "zendframework/zend-stdlib": "^2.7 || ^3.0" - }, - "suggest": { - "zendframework/zend-json-server": "For implementing JSON-RPC servers", - "zendframework/zend-xml2json": "For converting XML documents to JSON" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.0-dev", - "dev-develop": "3.1-dev" - } - }, - "autoload": { - "psr-4": { - "Zend\\Json\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "description": "provides convenience methods for serializing native PHP to JSON and decoding JSON to native PHP", - "homepage": "https://github.com/zendframework/zend-json", - "keywords": [ - "json", - "zf2" - ], - "time": "2016-04-01 02:34:00" - }, { "name": "zendframework/zend-servicemanager", "version": "3.1.1", @@ -679,16 +573,16 @@ }, { "name": "friendsofphp/php-cs-fixer", - "version": "v1.11.5", + "version": "v1.11.6", "source": { "type": "git", "url": "https://github.com/FriendsOfPHP/PHP-CS-Fixer.git", - "reference": "d3d08b76753092a232a4d8c3b94095ac06898719" + "reference": "41dc93abd2937a85a3889e28765231d574d2bac8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/FriendsOfPHP/PHP-CS-Fixer/zipball/d3d08b76753092a232a4d8c3b94095ac06898719", - "reference": "d3d08b76753092a232a4d8c3b94095ac06898719", + "url": "https://api.github.com/repos/FriendsOfPHP/PHP-CS-Fixer/zipball/41dc93abd2937a85a3889e28765231d574d2bac8", + "reference": "41dc93abd2937a85a3889e28765231d574d2bac8", "shasum": "" }, "require": { @@ -733,7 +627,7 @@ } ], "description": "A tool to automatically fix PHP code style", - "time": "2016-07-06 22:49:35" + "time": "2016-07-22 06:46:28" }, { "name": "myclabs/deep-copy", @@ -1231,16 +1125,16 @@ }, { "name": "phpunit/phpunit", - "version": "5.4.6", + "version": "5.4.7", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "2f1fc94b77ea6418bd6a06c64a1dac0645fbce59" + "reference": "6c8a756c17a1a92a066c99860eb57922e8b723da" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/2f1fc94b77ea6418bd6a06c64a1dac0645fbce59", - "reference": "2f1fc94b77ea6418bd6a06c64a1dac0645fbce59", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/6c8a756c17a1a92a066c99860eb57922e8b723da", + "reference": "6c8a756c17a1a92a066c99860eb57922e8b723da", "shasum": "" }, "require": { @@ -1305,7 +1199,7 @@ "testing", "xunit" ], - "time": "2016-06-16 06:01:15" + "time": "2016-07-21 06:55:27" }, { "name": "phpunit/phpunit-mock-objects", diff --git a/index.php b/index.php index e2ae57f..9acbbca 100644 --- a/index.php +++ b/index.php @@ -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"])); } } diff --git a/shariff.json b/shariff.json deleted file mode 100644 index afa0ad8..0000000 --- a/shariff.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "cache": { - "ttl": 60 - }, - "domains": [ - "www.heise.de", - "www.ct.de" - ], - "services": [ - "GooglePlus", - "Facebook", - "LinkedIn", - "Reddit", - "StumbleUpon", - "Flattr", - "Pinterest", - "Xing", - "AddThis" - ] -} diff --git a/src/Backend/BackendManager.php b/src/Backend/BackendManager.php index afdf124..c768fdc 100644 --- a/src/Backend/BackendManager.php +++ b/src/Backend/BackendManager.php @@ -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( @@ -116,7 +116,7 @@ function ($service) use ($url) { $this->services ); - /** @var ResponseInterface[] $results */ + /** @var ResponseInterface[]|TransferException[] $results */ $results = Pool::batch($this->client, $requests); $counts = []; @@ -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; } diff --git a/src/Backend/Facebook.php b/src/Backend/Facebook.php index 9220a47..5dad622 100644 --- a/src/Backend/Facebook.php +++ b/src/Backend/Facebook.php @@ -61,6 +61,6 @@ protected function getAccessToken() return $response->getBody(); } - return; + return null; } } diff --git a/src/ZendCache.php b/src/ZendCache.php index ad4d288..a6f5f32 100644 --- a/src/ZendCache.php +++ b/src/ZendCache.php @@ -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;