Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Optimize #108

Merged
merged 30 commits into from
Sep 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
707a978
take changes from crossjoin/browscap
mimmi20 Aug 31, 2015
0e50673
revert some changes
mimmi20 Sep 1, 2015
0496556
updates
mimmi20 Sep 2, 2015
07aff37
add php nightly to tests
mimmi20 Sep 4, 2015
291ba09
change detection
mimmi20 Sep 5, 2015
a7784d7
update from 3.x
mimmi20 Sep 6, 2015
a246477
remove include
mimmi20 Sep 7, 2015
dfeeaea
Merge branch 'master' of https://github.com/browscap/browscap-php int…
mimmi20 Sep 7, 2015
3e92776
update from master
mimmi20 Sep 8, 2015
7bae99d
update
mimmi20 Sep 8, 2015
737688e
revert change
mimmi20 Sep 8, 2015
c2fb053
revert change
mimmi20 Sep 8, 2015
dc24b39
update converter
mimmi20 Sep 8, 2015
3326b47
change detection
mimmi20 Sep 9, 2015
d950d19
update from master
mimmi20 Sep 9, 2015
c40e2e1
change detection
mimmi20 Sep 10, 2015
92e8be8
Merge branch 'optimize' of https://github.com/mimmi20/browscap-php in…
mimmi20 Sep 10, 2015
8451b22
update
mimmi20 Sep 13, 2015
a313591
changes
mimmi20 Sep 14, 2015
f8783c9
update creating cache
mimmi20 Sep 15, 2015
6b882aa
extract DataInterface
mimmi20 Sep 16, 2015
16f2aed
fix coding style
mimmi20 Sep 16, 2015
575bba6
skipping out a test on hhvm
mimmi20 Sep 16, 2015
7f789e8
skipping out a test on hhvm
mimmi20 Sep 16, 2015
5064c6f
update dependency injection
mimmi20 Sep 17, 2015
a1e895f
Merge branch 'master' of https://github.com/browscap/browscap-php int…
mimmi20 Sep 18, 2015
a24bfff
change parsing ini files, change tests
mimmi20 Sep 20, 2015
ba1897b
fix coding style
mimmi20 Sep 20, 2015
1968225
Skip code coverage generation on HHVM
mimmi20 Sep 25, 2015
8760732
change comment
mimmi20 Sep 26, 2015
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
15 changes: 9 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
*~
.settings
vendor/
.idea/
.buildpath
.settings
.project
.idea/
vendor/
phpunit.xml
composer.phar
composer.lock
resources/
PHP_BrowscapINI
resources
ergebnis.txt
tests/data/*
!tests/data/testdata.ini
!tests/data/.gitkeep
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ before_script:
- travis_retry composer install -o --prefer-source

script:
- php -d browscap=$TRAVIS_BUILD_DIR/resources/browscap.ini vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover --colors --verbose
# xdebug is not installed on hhvm, so we are skipping the creation of code coverage data there
- if [ "`phpenv version-name`" != "hhvm" ]; then php -d browscap=$TRAVIS_BUILD_DIR/resources/browscap.ini vendor/bin/phpunit --colors --verbose --exclude-group compare --coverage-text --coverage-clover=coverage.clover; fi
- if [ "`phpenv version-name`" == "hhvm" ]; then php -d browscap=$TRAVIS_BUILD_DIR/resources/browscap.ini vendor/bin/phpunit --colors --verbose --exclude-group compare; fi
- php -d browscap=$TRAVIS_BUILD_DIR/resources/browscap.ini vendor/bin/phpunit --colors --verbose --group compare
- php vendor/bin/phpcs --standard=psr2 -pn src tests

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
# xdebug is not installed on hhvm, so we have no code coverage data to send
- if [ "`phpenv version-name`" != "hhvm" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "`phpenv version-name`" != "hhvm" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi

53 changes: 18 additions & 35 deletions src/Browscap.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
use BrowscapPHP\Helper\Converter;
use BrowscapPHP\Helper\Filesystem;
use BrowscapPHP\Helper\IniLoader;
use BrowscapPHP\Helper\Quoter;
use BrowscapPHP\Parser\ParserInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use WurflCache\Adapter\AdapterInterface;
Expand Down Expand Up @@ -80,7 +82,9 @@ class Browscap
*/
private $cache = null;

/** @var \Psr\Log\LoggerInterface */
/**
* @var @var \Psr\Log\LoggerInterface
*/
private $logger = null;

/**
Expand Down Expand Up @@ -174,7 +178,7 @@ public function setCache($cache)
*
* @return \BrowscapPHP\Browscap
*/
public function setParser(Parser\ParserInterface $parser)
public function setParser(ParserInterface $parser)
{
$this->parser = $parser;

Expand All @@ -184,27 +188,20 @@ public function setParser(Parser\ParserInterface $parser)
/**
* returns an instance of the used parser class
*
* @return Parser\ParserInterface
* @return \BrowscapPHP\Parser\ParserInterface
*/
public function getParser()
{
if (null === $this->parser) {
$this->setParser(new Parser\Ini());
}
$cache = $this->getCache();
$logger = $this->getLogger();
$quoter = new Quoter();

$helper = new Parser\Helper\GetPattern();
$patternHelper = new Parser\Helper\GetPattern($cache, $logger);
$dataHelper = new Parser\Helper\GetData($cache, $logger, $quoter);

$helper
->setCache($this->getCache())
->setLogger($this->getLogger())
;

$this->parser
->setHelper($helper)
->setFormatter($this->getFormatter())
->setCache($this->getCache())
->setLogger($this->getLogger())
;
$this->parser = new Parser\Ini($patternHelper, $dataHelper, $this->getFormatter());
}

return $this->parser;
}
Expand Down Expand Up @@ -319,11 +316,7 @@ public function convertFile($iniFile)
throw new Exception('an error occured while setting the local file', 0, $e);
}

$converter = new Converter();
$converter
->setLogger($this->getLogger())
->setCache($this->getCache())
;
$converter = new Converter($this->getLogger(), $this->getCache());

try {
$converter->convertString($loader->load());
Expand All @@ -339,13 +332,8 @@ public function convertFile($iniFile)
*/
public function convertString($iniString)
{
$converter = new Converter();

$converter
->setLogger($this->getLogger())
->setCache($this->getCache())
->convertString($iniString)
;
$converter = new Converter($this->getLogger(), $this->getCache());
$converter->convertString($iniString);
}

/**
Expand Down Expand Up @@ -398,12 +386,7 @@ public function update($remoteFile = IniLoader::PHP_INI)
{
$this->getLogger()->debug('started fetching remote file');

$converter = new Converter();

$converter
->setLogger($this->getLogger())
->setCache($this->getCache())
;
$converter = new Converter($this->getLogger(), $this->getCache());

if (class_exists('\Browscap\Browscap')) {
$resourceFolder = 'vendor/browscap/browscap/resources/';
Expand Down
69 changes: 16 additions & 53 deletions src/Cache/BrowscapCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,39 +71,12 @@ class BrowscapCache
* if needed updated the definitions
*
* @param \WurflCache\Adapter\AdapterInterface $adapter
*
* @throws \BrowscapPHP\Exception
*/
public function __construct(AdapterInterface $adapter)
{
$this
->setCacheAdapter($adapter)
->setUpdateInterval(self::CACHE_LIVETIME)
;
}

/**
* sets the cache adapter
*
* @param \WurflCache\Adapter\AdapterInterface $adapter
*
* @return \BrowscapPHP\Cache\BrowscapCache
* @param int $updateInterval
*/
public function setCacheAdapter(AdapterInterface $adapter)
public function __construct(AdapterInterface $adapter, $updateInterval = self::CACHE_LIVETIME)
{
$this->cache = $adapter;

return $this;
}

/**
* returns the cache adapter
*
* @return \WurflCache\Adapter\AdapterInterface
*/
public function getCacheAdapter()
{
return $this->cache;
$this->cache->setExpiration((int) $updateInterval);
}

/**
Expand All @@ -114,7 +87,7 @@ public function getCacheAdapter()
public function getVersion()
{
if ($this->version === null) {
$success = null;
$success = true;

$version = $this->getItem('browscap.version', false, $success);

Expand All @@ -126,20 +99,6 @@ public function getVersion()
return $this->version;
}

/**
* set the update intervall
*
* @param integer $updateInterval
*
* @return \BrowscapPHP\Cache\BrowscapCache
*/
public function setUpdateInterval($updateInterval)
{
$this->getCacheAdapter()->setExpiration((int) $updateInterval);

return $this;
}

/**
* Get an item.
*
Expand All @@ -155,16 +114,20 @@ public function getItem($cacheId, $withVersion = true, & $success = null)
$cacheId .= '.'.$this->getVersion();
}

if (!$this->getCacheAdapter()->hasItem($cacheId)) {
if (!$this->cache->hasItem($cacheId)) {
$success = false;

return null;
}

$success = null;
$data = $this->getCacheAdapter()->getItem($cacheId, $success);
$data = $this->cache->getItem($cacheId, $success);

$success = true;
if (!isset($data['content'])) {
$success = false;

return null;
}

return unserialize($data['content']);
}
Expand All @@ -182,15 +145,15 @@ public function setItem($cacheId, $content, $withVersion = true)
{
// Get the whole PHP code
$data = array(
'content' => serialize($content),
'content' => serialize($content),
);

if ($withVersion) {
$cacheId .= '.'.$this->getVersion();
}

// Save and return
return $this->getCacheAdapter()->setItem($cacheId, $data);
return $this->cache->setItem($cacheId, $data);
}

/**
Expand All @@ -207,7 +170,7 @@ public function hasItem($cacheId, $withVersion = true)
$cacheId .= '.'.$this->getVersion();
}

return $this->getCacheAdapter()->hasItem($cacheId);
return $this->cache->hasItem($cacheId);
}

/**
Expand All @@ -224,7 +187,7 @@ public function removeItem($cacheId, $withVersion = true)
$cacheId .= '.'.$this->getVersion();
}

return $this->getCacheAdapter()->removeItem($cacheId);
return $this->cache->removeItem($cacheId);
}

/**
Expand All @@ -234,6 +197,6 @@ public function removeItem($cacheId, $withVersion = true)
*/
public function flush()
{
return $this->getCacheAdapter()->flush();
return $this->cache->flush();
}
}
98 changes: 98 additions & 0 deletions src/Data/PropertyFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright (c) 1998-2015 Browser Capabilities Project
*
* 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.
*
* @category Browscap-PHP
* @package Parser
* @copyright 1998-2015 Browser Capabilities Project
* @license http://www.opensource.org/licenses/MIT MIT License
* @link https://github.com/browscap/browscap-php/
* @since added with version 3.0
*/

namespace BrowscapPHP\Data;

/**
* Ini parser class (compatible with PHP 5.3+)
*
* @category Browscap-PHP
* @package Parser
* @author Christoph Ziegenberg <[email protected]>
* @author Thomas Müller <[email protected]>
* @copyright Copyright (c) 1998-2014 Browser Capabilities Project
* @version 3.0
* @license http://www.opensource.org/licenses/MIT MIT License
* @link https://github.com/browscap/browscap-php/
*/
class PropertyFormatter
{
/**
* @var PropertyHolder
*/
private $propertyHolder = null;

/**
* class constructor
*
* @param PropertyHolder $propertyHolder
*/
public function __construct(PropertyHolder $propertyHolder)
{
$this->propertyHolder = $propertyHolder;
}

/**
* formats the name of a property
*
* @param string $value
* @param string $property
*
* @return string
*/
public function formatPropertyValue($value, $property)
{
$valueOutput = $value;

switch ($this->propertyHolder->getPropertyType($property)) {
case PropertyHolder::TYPE_BOOLEAN:
if (true === $value || $value === 'true' || $value === '1') {
$valueOutput = true;
} elseif (false === $value || $value === 'false' || $value === '') {
$valueOutput = false;
} else {
$valueOutput = '';
}
break;
case PropertyHolder::TYPE_IN_ARRAY:
try {
$valueOutput = $this->propertyHolder->checkValueInArray($property, $value);
} catch (\InvalidArgumentException $ex) {
$valueOutput = '';
}
break;
default:
// nothing t do here
break;
}

return $valueOutput;
}
}
1 change: 1 addition & 0 deletions src/Data/PropertyHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function getPropertyType($propertyName)
'RenderingEngine_Maker',
'Parent',
'PropertyName',
'CDF',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is CDF?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asgrim CDF is a property which was part of the browscap.ini some time ago, I asume before you took over the project lead. I dont known what whis property is about.
If you are installing an Xampp like I did an outdated browscap.ini was part of that installation.
Adding this property here should make sure, that the unittests for this library do not fail with an outdated ni file.

);

if (in_array($propertyName, $stringProperties)) {
Expand Down
Loading