Skip to content

Commit

Permalink
Merge pull request #44 from fntlnz/upgrade-dependencies
Browse files Browse the repository at this point in the history
Upgraded dependencies
  • Loading branch information
leogr committed Mar 26, 2016
2 parents f9d7550 + f9ab94a commit 7e3cb1d
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 136 deletions.
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ language: php
php:
- 5.5
- 5.6
- 7
- 7.0
- hhvm

env:
matrix:
- DEPENDENCIES=""
- DEPENDENCIES="--prefer-lowest --prefer-stable"

branches:
only:
- master
- develop

install:
- composer self-update
- composer install --prefer-source
- composer update --prefer-source $DEPENDENCIES

before_script:
- mkdir -p build/coverage
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"homepage": "https://github.com/ripaclub/sphinxsearch",
"license": "BSD-2-Clause",
"require": {
"php": ">=5.5",
"zendframework/zend-db": "~2.4",
"zendframework/zend-servicemanager": "~2.4",
"zendframework/zend-stdlib": "~2.4"
"php": "^5.5 || ^7.0",
"container-interop/container-interop": "~1.0",
"zendframework/zend-db": "^2.4 || ^3.0",
"zendframework/zend-servicemanager": "^2.4 || ^3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.3",
Expand Down
111 changes: 63 additions & 48 deletions library/Db/Adapter/AdapterAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
*/
namespace SphinxSearch\Db\Adapter;

use SphinxSearch\Db\Adapter\Driver\Pdo\Statement as PdoStatement;
use SphinxSearch\Db\Adapter\Exception\UnsupportedDriverException;
use SphinxSearch\Db\Adapter\Platform\SphinxQL;
use Zend\Db\Adapter\Adapter as ZendDBAdapter;
use Zend\Db\Adapter\Driver\Mysqli\Mysqli as ZendMysqliDriver;
use Zend\Db\Adapter\Driver\Pdo\Pdo as ZendPdoDriver;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;

/**
* Class AdapterAbstractServiceFactory
Expand All @@ -33,36 +30,14 @@ class AdapterAbstractServiceFactory implements AbstractFactoryInterface
* @var array
*/
protected $config;

/**
* Can we create an adapter by the requested name?
*
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$config = $this->getConfig($services);
if (empty($config)) {
return false;
}

return (
isset($config[$requestedName])
// && is_array($config[$requestedName]) // Omitted because could be a driver instance
&& !empty($config[$requestedName])
);
}


/**
* Get db configuration, if any
*
* @param ServiceLocatorInterface $services
* @param ServiceLocatorInterface|ContainerInterface $services
* @return array
*/
protected function getConfig(ServiceLocatorInterface $services)
protected function getConfig($services)
{
if ($this->config !== null) {
return $this->config;
Expand Down Expand Up @@ -96,6 +71,44 @@ protected function getConfig(ServiceLocatorInterface $services)

return $this->config;
}

/**
* @param ServiceLocatorInterface|ContainerInterface $services
* @param string $requestedName
* @return boolean
*/
protected function hasConfigForRequestedName($services, $requestedName)
{
// Workaround to avoid infinite loop when getService() tries to call $services->get('Config')
if ($requestedName === 'Config') {
return false;
}

$config = $this->getConfig($services);

if (empty($config)) {
return false;
}

return (
isset($config[$requestedName])
// && is_array($config[$requestedName]) // Omitted because could be a driver instance
&& !empty($config[$requestedName])
);
}

/**
* Can we create an adapter by the requested name?
*
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
return $this->hasConfigForRequestedName($services, $requestedName);
}

/**
* Create a DB adapter
Expand All @@ -109,24 +122,26 @@ protected function getConfig(ServiceLocatorInterface $services)
public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$config = $this->getConfig($services);

$platform = new SphinxQL();
$adapter = new ZendDBAdapter($config[$requestedName], $platform);
$driver = $adapter->getDriver();
// Check driver
if ($driver instanceof ZendPdoDriver &&
$driver->getDatabasePlatformName(ZendPdoDriver::NAME_FORMAT_CAMELCASE) == 'Mysql'
) {
$driver->registerStatementPrototype(new PdoStatement());
} elseif (!$driver instanceof ZendMysqliDriver) {
$class = get_class($driver);
throw new UnsupportedDriverException(
$class . ' not supported. Use Zend\Db\Adapter\Driver\Pdo\Pdo or Zend\Db\Adapter\Driver\Mysqli\Mysqli'
);
}

$platform->setDriver($adapter->getDriver());

return $adapter;
return AdapterServiceFactory::factory($config[$requestedName]);
}

/**
* {@inheritdoc}
*/
public function canCreate(ContainerInterface $container, $requestedName)
{
return $this->hasConfigForRequestedName($container, $requestedName);
}

/**
* {@inheritdoc}
* @return \Zend\Db\Adapter\Adapter
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $this->getConfig($container);
return AdapterServiceFactory::factory($config[$requestedName]);
}


}
62 changes: 46 additions & 16 deletions library/Db/Adapter/AdapterServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Zend\Db\Adapter\Driver\Pdo\Pdo as ZendPdoDriver;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;

/**
* Class AdapterServiceFactory
Expand All @@ -30,32 +31,61 @@
class AdapterServiceFactory implements FactoryInterface
{
/**
* Create db adapter service
*
* @param ServiceLocatorInterface $serviceLocator
* @throws Exception\UnsupportedDriverException
* Default configuration key
*
* @var string
*/
protected $configKey = 'sphinxql';

/**
* @param array|\Zend\Db\Adapter\Driver\DriverInterface $config
* @throws UnsupportedDriverException
* @return \Zend\Db\Adapter\Adapter
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public static function factory($config)
{
$config = $serviceLocator->get('Config');
$platform = new SphinxQL();
$adapter = new ZendDBAdapter($config['sphinxql'], $platform);
$adapter = new ZendDBAdapter($config, $platform);
$driver = $adapter->getDriver();
// Check driver
if ($driver instanceof ZendPdoDriver &&
$driver->getDatabasePlatformName(ZendPdoDriver::NAME_FORMAT_CAMELCASE) == 'Mysql'
) {
$driver->registerStatementPrototype(new PdoStatement());
} elseif (!$driver instanceof ZendMysqliDriver) {
$class = get_class($driver);
throw new UnsupportedDriverException(
$class . ' not supported. Use Zend\Db\Adapter\Driver\Pdo\Pdo or Zend\Db\Adapter\Driver\Mysqli\Mysqli'
);
) {
$driver->registerStatementPrototype(new PdoStatement());
} elseif (!$driver instanceof ZendMysqliDriver) {
$class = get_class($driver);
throw new UnsupportedDriverException(
$class . ' not supported. Use Zend\Db\Adapter\Driver\Pdo\Pdo or Zend\Db\Adapter\Driver\Mysqli\Mysqli'
);
}

$platform->setDriver($adapter->getDriver());

return $adapter;
}

/**
* Create db adapter service
*
* @param ServiceLocatorInterface $serviceLocator
* @throws Exception\UnsupportedDriverException
* @return \Zend\Db\Adapter\Adapter
* @deprecated Use __invoke() instead.
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return self::factory($serviceLocator->get('Config')[$this->configKey]);
}

/**
* Create db adapter service
*
* {@inheritdoc}
* @return \Zend\Db\Adapter\Adapter
* @throws UnsupportedDriverException
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return self::factory($options ? $options : $container->get('Config')[$this->configKey]);
}
}
14 changes: 0 additions & 14 deletions phpmd.xml.dist

This file was deleted.

3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">library</directory>
<exclude>
<directory suffix=".php">library/Db/Sql/compatibility</directory>
</exclude>
</whitelist>
</filter>
<logging>
Expand Down
50 changes: 25 additions & 25 deletions tests/Db/Adapter/AdapterAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ public function testUnsupportedDriver()
*/
public function testNullConfig($service)
{
$sManager = new ServiceManager(
new Config(
[
'abstract_factories' => ['SphinxSearch\Db\Adapter\AdapterAbstractServiceFactory'],
]
)
);
$sManagerConfig = new Config(
[
'abstract_factories' => ['SphinxSearch\Db\Adapter\AdapterAbstractServiceFactory'],
]
);
$sManager = new ServiceManager();
$sManagerConfig->configureServiceManager($sManager);
$sManager->get($service);
}

Expand All @@ -139,13 +139,13 @@ public function testNullConfig($service)
*/
public function testEmptyConfig($service)
{
$sManager = new ServiceManager(
new Config(
[
'abstract_factories' => ['SphinxSearch\Db\Adapter\AdapterAbstractServiceFactory'],
]
)
$sManagerConfig = new Config(
[
'abstract_factories' => ['SphinxSearch\Db\Adapter\AdapterAbstractServiceFactory'],
]
);
$sManager = new ServiceManager();
$sManagerConfig->configureServiceManager($sManager);
$sManager->setService('Config', []);
$sManager->get($service);
}
Expand All @@ -158,13 +158,13 @@ public function testEmptyConfig($service)
*/
public function testEmptySphinxQLConfig($service)
{
$sManager = new ServiceManager(
new Config(
[
'abstract_factories' => ['SphinxSearch\Db\Adapter\AdapterAbstractServiceFactory'],
]
)
$sManagerConfig = new Config(
[
'abstract_factories' => ['SphinxSearch\Db\Adapter\AdapterAbstractServiceFactory'],
]
);
$sManager = new ServiceManager();
$sManagerConfig->configureServiceManager($sManager);
$sManager->setService(
'Config',
[
Expand All @@ -181,13 +181,13 @@ public function testEmptySphinxQLConfig($service)
*/
protected function setUp()
{
$this->serviceManager = new ServiceManager(
new Config(
[
'abstract_factories' => ['SphinxSearch\Db\Adapter\AdapterAbstractServiceFactory'],
]
)
$sManagerConfig = new Config(
[
'abstract_factories' => ['SphinxSearch\Db\Adapter\AdapterAbstractServiceFactory'],
]
);
$this->serviceManager = new ServiceManager();
$sManagerConfig->configureServiceManager($this->serviceManager);
$this->serviceManager->setService(
'Config',
[
Expand Down
Loading

0 comments on commit 7e3cb1d

Please sign in to comment.