From 1893d5fcfd098c8dd2b30c4eaab97a86891211a1 Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Mon, 24 Oct 2016 10:06:27 +0200 Subject: [PATCH] Fix CS --- .php_cs | 43 +++++ .styleci.yml | 7 + Api/Extensions/BaseExtension.php | 53 ++++--- CacheWarmer/ThriftCompileCacheWarmer.php | 47 +++--- Client/Client.php | 33 ++-- Client/HttpClient.php | 24 +-- Client/SocketClient.php | 45 +++--- Client/ThriftClient.php | 55 ++++--- Client/ThriftTestClient.php | 19 ++- Client/ThriftTestClientPhpunit.php | 15 +- Command/ClientTestCommand.php | 67 ++++---- Command/CompileCommand.php | 65 ++++---- Command/ServerCommand.php | 40 +++-- Compiler/ThriftCompiler.php | 109 +++++++------ Controller/ThriftController.php | 25 +-- DependencyInjection/Compiler/FactoryPass.php | 21 ++- DependencyInjection/Configuration.php | 43 +++-- .../OverblogThriftExtension.php | 33 ++-- Exception/CompilerException.php | 15 +- Exception/ConfigurationException.php | 29 ++-- Factory/ThriftFactory.php | 49 +++--- LICENSE | 21 +++ Listener/ClassLoaderListener.php | 30 ++-- OverblogThriftBundle.php | 17 +- README.md | 13 ++ Routing/ThriftRoutingLoader.php | 31 ++-- Server/HttpServer.php | 19 ++- Server/Server.php | 29 ++-- Server/SocketServer.php | 27 ++-- Tests/Client/TestHandler.php | 40 +++-- Tests/Client/ThriftClientTest.php | 147 +++++++++--------- Tests/Command/AppKernelMock.php | 18 ++- Tests/Command/CompileCommandTest.php | 29 ++-- Tests/Command/ServerTest.php | 18 ++- Tests/Compiler/CompilerTest.php | 36 +++-- Tests/Factory/ThriftFactoryTest.php | 26 +++- Tests/ThriftBundleTestCase.php | 15 +- Tests/bootstrap.php | 9 ++ composer.json | 16 +- 39 files changed, 867 insertions(+), 511 deletions(-) create mode 100644 .php_cs create mode 100644 .styleci.yml create mode 100644 LICENSE diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..40801e6 --- /dev/null +++ b/.php_cs @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require_once __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php'; + +use SLLH\StyleCIBridge\ConfigBridge; +use Symfony\CS\Fixer\Contrib\HeaderCommentFixer; + +$header = << + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. +EOF; + +// PHP-CS-Fixer 1.x +if (method_exists('Symfony\CS\Fixer\Contrib\HeaderCommentFixer', 'getHeader')) { + HeaderCommentFixer::setHeader($header); +} + +$config = ConfigBridge::create(); + +// PHP-CS-Fixer 2.x +if (method_exists($config, 'setRules')) { + $config->setRules(array_merge($config->getRules(), [ + 'header_comment' => ['header' => $header] + ])); +} + +return $config + ->setUsingCache(true) + ->fixers(array_merge($config->getFixers(), ['header_comment'])) +; diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..013c147 --- /dev/null +++ b/.styleci.yml @@ -0,0 +1,7 @@ +preset: symfony +enabled: + - ordered_use + - short_array_syntax + +disabled: + - unalign_equals diff --git a/Api/Extensions/BaseExtension.php b/Api/Extensions/BaseExtension.php index 5f9c77f..3355720 100644 --- a/Api/Extensions/BaseExtension.php +++ b/Api/Extensions/BaseExtension.php @@ -1,49 +1,56 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + /** - * Symfony extension for Thrift Extension + * Symfony extension for Thrift Extension. * * @category Bundle - * @package InternalApi + * * @author Vincent Bouzeran * @author Yannick Le Guédart * @copyright 2011 Overblog */ - namespace Overblog\ThriftBundle\Api\Extensions; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Symfony extension for Thrift Extension + * Symfony extension for Thrift Extension. * * @category Bundle - * @package InternalApi - * @subpackage Base extension + * * @author Vincent Bouzeran * @author Yannick Le Guédart */ - abstract class BaseExtension { /** - * Injected SF2 container + * Injected SF2 container. * * @var type */ protected $_container; /** - * Thrift Factory + * Thrift Factory. + * * @var type */ protected $factory; /** - * Constructor + * Constructor. * * @param ContainerInterface $container */ - public function __construct(ContainerInterface $container) { $this->_container = $container; @@ -51,32 +58,30 @@ public function __construct(ContainerInterface $container) } /** - * Returns a service from the injected container + * Returns a service from the injected container. * * @param string $service * * @return mixed */ - public function get($service) { return $this->_container->get($service); } /** - * Set a service to the injected container + * Set a service to the injected container. * - * @param string $id, The service id - * @param mixed $service, The service + * @param string $id, The service id + * @param mixed $service, The service */ - public function set($id, $service) { $this->_container->set($id, $service); } /** - * Returns a parameter from the injected container + * Returns a parameter from the injected container. * * @param string $name * @@ -88,13 +93,15 @@ public function getParameter($name) } /** - * Get instance of Thrift Model classes - * @param string $classe - * @param mixed $param + * Get instance of Thrift Model classes. + * + * @param string $class + * @param mixed $param + * * @return mixed */ - public function getInstance($classe, $param = null) + public function getInstance($class, $param = null) { - return $this->factory->getInstance($classe, $param); + return $this->factory->getInstance($class, $param); } } diff --git a/CacheWarmer/ThriftCompileCacheWarmer.php b/CacheWarmer/ThriftCompileCacheWarmer.php index e06f831..45a95be 100755 --- a/CacheWarmer/ThriftCompileCacheWarmer.php +++ b/CacheWarmer/ThriftCompileCacheWarmer.php @@ -1,18 +1,26 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\ClassLoader\ClassMapGenerator; +namespace Overblog\ThriftBundle\CacheWarmer; use Overblog\ThriftBundle\Compiler\ThriftCompiler; use Overblog\ThriftBundle\Exception\CompilerException; +use Symfony\Component\ClassLoader\ClassMapGenerator; +use Symfony\Component\Filesystem\Filesystem; /** - * Generate Thrift model in cache warmer + * Generate Thrift model in cache warmer. + * * @author Xavier HAUSHERR */ - class ThriftCompileCacheWarmer { private $rootDir; @@ -21,18 +29,19 @@ class ThriftCompileCacheWarmer private $services; /** - * Cache Suffix for thrift compiled files + * Cache Suffix for thrift compiled files. */ const CACHE_SUFFIX = 'thrift'; /** - * Register dependencies + * Register dependencies. + * * @param string $cacheDir * @param string $rootDir - * @param Array $path - * @param Array $services + * @param array $path + * @param array $services */ - public function __construct($cacheDir, $rootDir, $path, Array $services) + public function __construct($cacheDir, $rootDir, $path, array $services) { $this->cacheDir = $cacheDir; $this->rootDir = $rootDir; @@ -41,10 +50,13 @@ public function __construct($cacheDir, $rootDir, $path, Array $services) } /** - * Return definition path + * Return definition path. + * * @param string $definition * @param string $definitionPath + * * @throws \Exception + * * @return string */ public function getDefinitionPath($definition, $definitionPath) @@ -57,7 +69,7 @@ public function getDefinitionPath($definition, $definitionPath) } /** - * Compile Thrift Model + * Compile Thrift Model. */ public function compile() { @@ -66,8 +78,7 @@ public function compile() $cacheDir = sprintf('%s/%s', $this->cacheDir, self::CACHE_SUFFIX); // We compile for every Service - foreach($this->services as $config) - { + foreach ($this->services as $config) { $definitionPath = $this->getDefinitionPath( $config['definition'], $config['definitionPath'] @@ -87,8 +98,7 @@ public function compile() $compile = $compiler->compile($definitionPath, $config['server']); // Compilation Error - if(false === $compile) - { + if (false === $compile) { throw new \RuntimeException( sprintf('Unable to compile Thrift definition %s.', $definitionPath), 0, @@ -100,12 +110,11 @@ public function compile() // Check if thrift cache directory exists $fs = new Filesystem(); - if(!$fs->exists($cacheDir)) - { + if (!$fs->exists($cacheDir)) { $fs->mkdir($cacheDir); } // Generate ClassMap ClassMapGenerator::dump($cacheDir, sprintf('%s/classes.map', $cacheDir)); } -} \ No newline at end of file +} diff --git a/Client/Client.php b/Client/Client.php index 6aef9e9..d2a3bca 100644 --- a/Client/Client.php +++ b/Client/Client.php @@ -1,44 +1,55 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Client; /** - * Abstract class for create a client + * Abstract class for create a client. + * * @author Xavier HAUSHERR */ - abstract class Client { /** - * Config handler + * Config handler. + * * @var array */ protected $config; /** - * Socket instance + * Socket instance. + * * @var Thrift\Transport\TSocket */ protected $socket; /** - * Register dependencies + * Register dependencies. + * * @param array $config */ - public function __construct(Array $config) + public function __construct(array $config) { $this->config = $config; } /** - * Return socket + * Return socket. * * @return Thrift\Transport\TSocket */ public function getSocket() { - if(is_null($this->socket)) - { + if (is_null($this->socket)) { $this->socket = $this->createSocket(); } @@ -46,9 +57,9 @@ public function getSocket() } /** - * Insctanciate socket + * Insctanciate socket. * * @return Thrift\Transport\TSocket */ abstract protected function createSocket(); -} \ No newline at end of file +} diff --git a/Client/HttpClient.php b/Client/HttpClient.php index 0716e05..4f21d61 100644 --- a/Client/HttpClient.php +++ b/Client/HttpClient.php @@ -1,20 +1,27 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ -use Overblog\ThriftBundle\Client\Client; +namespace Overblog\ThriftBundle\Client; use Thrift\Transport\THttpClient; /** - * HTTP Client + * HTTP Client. + * * @author Xavier HAUSHERR */ - class HttpClient extends Client { /** - * Instanciate Socket Client + * Instanciate Socket Client. * * @return Thrift\Transport\THttpClient */ @@ -22,15 +29,14 @@ protected function createSocket() { $host = current($this->config['hosts']); - $url = parse_url($this->config['type'] . '://' . $host['host']); + $url = parse_url($this->config['type'].'://'.$host['host']); $socket = new THttpClient($url['host'], $host['port'], $url['path']); - if (!empty($host['httpTimeoutSecs'])) - { + if (!empty($host['httpTimeoutSecs'])) { $socket->setTimeoutSecs($host['httpTimeoutSecs']); } return $socket; } -} \ No newline at end of file +} diff --git a/Client/SocketClient.php b/Client/SocketClient.php index 9f95520..56c5c8c 100644 --- a/Client/SocketClient.php +++ b/Client/SocketClient.php @@ -1,21 +1,28 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ -use Overblog\ThriftBundle\Client\Client; +namespace Overblog\ThriftBundle\Client; use Thrift\Transport\TSocket; use Thrift\Transport\TSocketPool; /** - * Socket Client + * Socket Client. + * * @author Xavier HAUSHERR */ - class SocketClient extends Client { /** - * Instanciate Socket Client + * Instanciate Socket Client. * * @return Thrift\Transport\TSocket */ @@ -23,34 +30,34 @@ protected function createSocket() { $nbHosts = count($this->config['hosts']); - if($nbHosts == 1) - { + if ($nbHosts == 1) { $host = current($this->config['hosts']); $socket = new TSocket($host['host'], $host['port']); - if (!empty($host['recvTimeout'])) + if (!empty($host['recvTimeout'])) { $socket->setRecvTimeout($host['recvTimeout']); - if (!empty($host['sendTimeout'])) + } + if (!empty($host['sendTimeout'])) { $socket->setSendTimeout($host['sendTimeout']); - } - else - { - $hosts = array(); - $ports = array(); + } + } else { + $hosts = []; + $ports = []; - foreach($this->config['hosts'] as $host) - { + foreach ($this->config['hosts'] as $host) { $hosts[] = $host['host']; $ports[] = $host['port']; } $socket = new TSocketPool($hosts, $ports); - if (!empty($host['recvTimeout'])) + if (!empty($host['recvTimeout'])) { $socket->setRecvTimeout($host['recvTimeout']); - if (!empty($host['sendTimeout'])) + } + if (!empty($host['sendTimeout'])) { $socket->setSendTimeout($host['sendTimeout']); + } } return $socket; } -} \ No newline at end of file +} diff --git a/Client/ThriftClient.php b/Client/ThriftClient.php index 7ac8c95..9f613da 100755 --- a/Client/ThriftClient.php +++ b/Client/ThriftClient.php @@ -1,58 +1,70 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Client; -use Thrift\Transport\TBufferedTransport; use Overblog\ThriftBundle\Factory\ThriftFactory; +use Thrift\Transport\TBufferedTransport; /** - * Build client + * Build client. + * * @author Xavier HAUSHERR */ - class ThriftClient { /** - * Thrift factory + * Thrift factory. + * * @var ThriftFactory */ protected $factory; /** - * Client config + * Client config. + * * @var array */ - protected $config = array(); + protected $config = []; /** - * Client instance + * Client instance. */ protected $client; /** - * Transport instance + * Transport instance. */ protected $transport; /** - * Register Dependencies + * Register Dependencies. + * * @param \Overblog\ThriftBundle\Factory\ThriftFactory $factory - * @param array $config + * @param array $config */ - public function __construct(ThriftFactory $factory, Array $config) + public function __construct(ThriftFactory $factory, array $config) { $this->factory = $factory; $this->config = $config; } /** - * Return client + * Return client. + * * @return Thrift\Transport\TSocket */ public function getClient() { - if(is_null($this->client)) - { + if (is_null($this->client)) { $service = $this->config['service_config']; //Initialisation du client $socket = $this->clientFactory()->getSocket(); @@ -75,9 +87,11 @@ public function getClient() } /** - * Instanciate Thrift Model classes + * Instanciate Thrift Model classes. + * * @param string $classe - * @param mixed $param + * @param mixed $param + * * @return mixed */ public function getFactory($classe, $param = null) @@ -86,19 +100,20 @@ public function getFactory($classe, $param = null) } /** - * Close every connections + * Close every connections. */ public function __destruct() { - if(!is_null($this->transport)) - { + if (!is_null($this->transport)) { $this->transport->close(); } } /** - * Instanciate client class + * Instanciate client class. + * * @param string $name + * * @return Client */ protected function clientFactory() diff --git a/Client/ThriftTestClient.php b/Client/ThriftTestClient.php index bcf9725..df3d02c 100755 --- a/Client/ThriftTestClient.php +++ b/Client/ThriftTestClient.php @@ -1,24 +1,29 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ -use Overblog\ThriftBundle\Client\ThriftClient; -use Overblog\ThriftBundle\Client\ThriftTestClientPhpunit; +namespace Overblog\ThriftBundle\Client; /** - * ThriftTestClient is used to replace Thrift Client by a PhpUnit Mocked version + * ThriftTestClient is used to replace Thrift Client by a PhpUnit Mocked version. * * @author Xavier HAUSHERR */ class ThriftTestClient extends ThriftClient { /** - * @inheritdoc + * {@inheritdoc} */ public function getClient() { - if(is_null($this->client)) - { + if (is_null($this->client)) { $className = sprintf( '%s\%sClient', $this->config['service_config']['namespace'], diff --git a/Client/ThriftTestClientPhpunit.php b/Client/ThriftTestClientPhpunit.php index e07626e..0b23897 100755 --- a/Client/ThriftTestClientPhpunit.php +++ b/Client/ThriftTestClientPhpunit.php @@ -1,10 +1,21 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Client; /** - * PHPUnit Mock of a Thrift Client + * PHPUnit Mock of a Thrift Client. * * @author Xavier HAUSHERR */ -class ThriftTestClientPhpunit extends \PHPUnit_Framework_TestCase {} +class ThriftTestClientPhpunit extends \PHPUnit_Framework_TestCase +{ +} diff --git a/Command/ClientTestCommand.php b/Command/ClientTestCommand.php index 7e67359..c4d6c07 100755 --- a/Command/ClientTestCommand.php +++ b/Command/ClientTestCommand.php @@ -1,20 +1,29 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Command; +use Overblog\ThriftBundle\Client\ThriftClient; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; -use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Overblog\ThriftBundle\Client\ThriftClient; - class ClientTestCommand extends ContainerAwareCommand { protected function configure() - { + { $this->setName('thrift:client:test') - ->setDescription('Test Thrift methods'); + ->setDescription('Test Thrift methods'); $this->addArgument('service', InputArgument::REQUIRED, 'Service name'); $this->addArgument('method', InputArgument::REQUIRED, 'Method name'); @@ -24,72 +33,62 @@ protected function configure() $this->addOption('port', 'p', InputOption::VALUE_REQUIRED, 'Port to listen on', 9090); $this->addOption('recvTimeout', 'r', InputOption::VALUE_OPTIONAL, 'Data receive Timeout'); $this->addOption('mode', 'm', InputOption::VALUE_REQUIRED, 'Server mode (socket or http)', 'socket'); - } + } protected function execute(InputInterface $input, OutputInterface $output) - { + { $services = $this->getContainer()->getParameter('thrift.config.services'); $service = $input->getArgument('service'); $method = $input->getArgument('method'); $args = $input->getArgument('args'); - if(!isset($services[$service])) - { + if (!isset($services[$service])) { $output->writeln(sprintf('Unknow service: %s', $service)); + return false; } // Instantiate client $thriftClient = new ThriftClient( $this->getContainer()->get('thrift.factory'), - array( + [ 'service_config' => $services[$service], 'service' => $service, 'type' => $input->getOption('mode'), - 'hosts' => array( - $service => array( + 'hosts' => [ + $service => [ 'host' => $input->getOption('host'), 'port' => $input->getOption('port'), 'recvTimeout' => $input->getOption('recvTimeout'), - ) - ) - ) + ], + ], + ] ); $time_start = microtime(true); - try - { + try { $client = $thriftClient->getClient(); - if(count($args) > 0) - { - $result = call_user_func_array(array($client, $method), $args); - } - else - { - $result = call_user_func(array($client, $method)); + if (count($args) > 0) { + $result = call_user_func_array([$client, $method], $args); + } else { + $result = call_user_func([$client, $method]); } $end = (microtime(true) - $time_start); $output->writeln(print_r($result, true)); $output->writeln(sprintf('Time taken for request: %s ms', $end)); - - } - catch(\ThriftModel\User\InvalidValueException $e) - { + } catch (\ThriftModel\User\InvalidValueException $e) { $output->writeln(sprintf('Error Code: %s', $e->getCode())); $output->writeln(sprintf('%s', $e->getMessage())); - } - catch(\Thrift\Exception\TException $e) - { + } catch (\Thrift\Exception\TException $e) { $output->writeln(sprintf('%s', $e->getMessage())); $output->writeln(sprintf('Have you started the server with: php app/console thrift:server %s ?', $service)); + return false; - } - catch(\Exception $e) - { + } catch (\Exception $e) { $output->writeln(sprintf('%s', $e->getMessage())); } diff --git a/Command/CompileCommand.php b/Command/CompileCommand.php index 75c19a2..68da69a 100755 --- a/Command/CompileCommand.php +++ b/Command/CompileCommand.php @@ -1,29 +1,38 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Command; +use Overblog\ThriftBundle\CacheWarmer\ThriftCompileCacheWarmer; +use Overblog\ThriftBundle\Compiler\ThriftCompiler; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; -use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Overblog\ThriftBundle\Compiler\ThriftCompiler; -use Overblog\ThriftBundle\CacheWarmer\ThriftCompileCacheWarmer; - /** - * Compile command to generate thrift model + * Compile command to generate thrift model. + * * @author Xavier HAUSHERR */ - class CompileCommand extends ContainerAwareCommand { /** - * Configure the command + * Configure the command. */ protected function configure() - { + { $this->setName('thrift:compile') - ->setDescription('Compile Thrift Model for PHP'); + ->setDescription('Compile Thrift Model for PHP'); $this->addArgument('service', InputArgument::REQUIRED, 'Service name'); @@ -36,19 +45,19 @@ protected function configure() 'Bundle where the Model will be located (default is the same than the definitions'); $this->addOption('includeDir', 'I', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Add a directory to the list of directories searched for include directives'); - } + } /** - * Execute compilation - * @param InputInterface $input + * Execute compilation. + * + * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) - { + { $compiler = new ThriftCompiler(); - if(($path = $input->getOption('path'))) - { + if (($path = $input->getOption('path'))) { $compiler->setExecPath($path); } @@ -56,12 +65,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $configs = $this->getContainer()->getParameter('thrift.config.services'); // Get config - if(isset($configs[$service])) - { + if (isset($configs[$service])) { $config = $configs[$service]; - } - else - { + } else { $output->writeln(sprintf('Unknow service %s', $service)); return 1; @@ -75,15 +81,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $config['definitionPath'] ); - // Get out path - if(($bundleName = $input->getOption('bundleNameOut'))) - { + if (($bundleName = $input->getOption('bundleNameOut'))) { $bundle = $this->getContainer()->get('kernel')->getBundle($bundleName); $bundlePath = $bundle->getPath(); - } - else - { + } else { $bundlePath = getcwd(); } @@ -95,10 +97,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $compiler->setIncludeDirs($includeDirs); } - //Add namespace prefix if needed - if($input->getOption('namespace')) - { + if ($input->getOption('namespace')) { $compiler->setNamespacePrefix($input->getOption('namespace')); } @@ -109,12 +109,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $return = $compiler->compile($definitionPath, $input->getOption('server')); //Error - if(1 === $return) - { + if (1 === $return) { $output->writeln(sprintf('%s', implode("\n", $compiler->getLastOutput()))); - } - else - { + } else { $output->writeln(sprintf('%s', implode("\n", $compiler->getLastOutput()))); } } diff --git a/Command/ServerCommand.php b/Command/ServerCommand.php index d922828..7caca29 100644 --- a/Command/ServerCommand.php +++ b/Command/ServerCommand.php @@ -1,50 +1,60 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Command; +use Overblog\ThriftBundle\Server\SocketServer; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; -use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Overblog\ThriftBundle\Server\SocketServer; - /** - * Socket server command + * Socket server command. + * * @author Xavier HAUSHERR */ - class ServerCommand extends ContainerAwareCommand { /** - * Configure the command + * Configure the command. */ protected function configure() - { + { $this->setName('thrift:server') - ->setDescription('Start Thrift Server'); + ->setDescription('Start Thrift Server'); $this->addArgument('config', InputArgument::REQUIRED, 'Config key'); $this->addOption('host', 't', InputOption::VALUE_REQUIRED, 'Host to listen on', 'localhost'); $this->addOption('port', 'p', InputOption::VALUE_REQUIRED, 'Port to listen on', 9090); - } + } /** - * Execute server - * @param InputInterface $input + * Execute server. + * + * @param InputInterface $input * @param OutputInterface $output + * * @return bool */ protected function execute(InputInterface $input, OutputInterface $output) - { + { $servers = $this->getContainer()->getParameter('thrift.config.servers'); $config = $input->getArgument('config'); - if(!isset($servers[$config])) - { + if (!isset($servers[$config])) { $output->writeln(sprintf('Unknow service: %s', $config)); + return false; } @@ -64,4 +74,4 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } -} \ No newline at end of file +} diff --git a/Compiler/ThriftCompiler.php b/Compiler/ThriftCompiler.php index da8594c..b2bd81b 100644 --- a/Compiler/ThriftCompiler.php +++ b/Compiler/ThriftCompiler.php @@ -1,70 +1,87 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Compiler; use Overblog\ThriftBundle\Exception\ConfigurationException; /** - * Thrift compiler + * Thrift compiler. + * * @author Xavier HAUSHERR */ - class ThriftCompiler { /** - * Thrift Executable name + * Thrift Executable name. + * * @var string */ protected $thriftExec = 'thrift'; /** - * Thrift Executable path + * Thrift Executable path. + * * @var string */ protected $thriftPath = '/usr/local/bin/'; /** - * Model Path + * Model Path. + * * @var string */ protected $modelPath; /** - * Included dirs + * Included dirs. + * * @var string[] */ - protected $includeDirs = array(); + protected $includeDirs = []; /** - * Base compiler options + * Base compiler options. + * * @var array */ - protected $options = array('oop' => null); + protected $options = ['oop' => null]; /** - * Last compiler output + * Last compiler output. + * * @var string */ protected $lastOutput; /** - * Return Thrift path + * Return Thrift path. + * * @return string */ protected function getExecPath() { - return $this->thriftPath . $this->thriftExec; + return $this->thriftPath.$this->thriftExec; } /** - * Set exec path + * Set exec path. + * * @param string $path + * * @return bool */ public function setExecPath($path) { - if('/' !== substr($path, -1)) - { + if ('/' !== substr($path, -1)) { $path .= '/'; } @@ -74,14 +91,15 @@ public function setExecPath($path) } /** - * Check if Thrift exec is installed + * Check if Thrift exec is installed. + * * @throws \Overblog\ThriftBundle\Exception\ConfigurationException - * @return boolean + * + * @return bool */ protected function checkExec() { - if(!file_exists($this->getExecPath())) - { + if (!file_exists($this->getExecPath())) { throw new ConfigurationException('Unable to find Thrift executable'); } @@ -89,13 +107,13 @@ protected function checkExec() } /** - * Set model path and create it if needed + * Set model path and create it if needed. + * * @param string $path */ public function setModelPath($path) { - if(!is_null($path) && !file_exists($path)) - { + if (!is_null($path) && !file_exists($path)) { mkdir($path); } @@ -103,16 +121,18 @@ public function setModelPath($path) } /** - * Add a directory to the list of directories searched for include directives + * Add a directory to the list of directories searched for include directives. + * * @param string[] $includeDirs */ public function setIncludeDirs($includeDirs) { - $this->includeDirs = (array)$includeDirs; + $this->includeDirs = (array) $includeDirs; } /** - * Set namespace prefix + * Set namespace prefix. + * * @param string $namespace */ public function setNamespacePrefix($namespace) @@ -121,7 +141,7 @@ public function setNamespacePrefix($namespace) } /** - * Generate PHP validator methods + * Generate PHP validator methods. */ public function addValidate() { @@ -129,56 +149,56 @@ public function addValidate() } /** - * Compile server files too (processor) + * Compile server files too (processor). */ protected function addServerCompile() { - if(!isset($this->options['server'])) - { + if (!isset($this->options['server'])) { $this->options['server'] = null; } } /** - * Compile the thrift options + * Compile the thrift options. + * * @return string */ protected function compileOptions() { - $return = array(); + $return = []; - foreach($this->options as $option => $value) - { - $return[] = $option . (!empty($value) ? '=' . $value : ''); + foreach ($this->options as $option => $value) { + $return[] = $option.(!empty($value) ? '='.$value : ''); } return implode(',', $return); } /** - * Compile the Thrift definition + * Compile the Thrift definition. + * * @param string $definition - * @param boolean $serverCompile + * @param bool $serverCompile + * * @throws \Overblog\ThriftBundle\Exception\ConfigurationException - * @return boolean + * + * @return bool */ public function compile($definition, $serverCompile = false) { // Check if definition file exists - if(!file_exists($definition)) - { + if (!file_exists($definition)) { throw new ConfigurationException(sprintf('Unable to find Thrift definition at path "%s"', $definition)); } - if(true === $serverCompile) - { + if (true === $serverCompile) { $this->addServerCompile(); } // prepare includeDirs $includeDirs = ''; foreach ($this->includeDirs as $includeDir) { - $includeDirs .= ' -I '. $includeDir; + $includeDirs .= ' -I '.$includeDir; } //Reset output @@ -199,11 +219,12 @@ public function compile($definition, $serverCompile = false) } /** - * Return the last compiler output + * Return the last compiler output. + * * @return string */ public function getLastOutput() { return $this->lastOutput; } -} \ No newline at end of file +} diff --git a/Controller/ThriftController.php b/Controller/ThriftController.php index 69d3c11..0dbb9b1 100644 --- a/Controller/ThriftController.php +++ b/Controller/ThriftController.php @@ -1,32 +1,39 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Overblog\ThriftBundle\Controller; use Overblog\ThriftBundle\Server\HttpServer; +use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; /** - * Http Server controller + * Http Server controller. + * * @author Xavier HAUSHERR */ - class ThriftController extends Controller { /** - * HTTP Entry point + * HTTP Entry point. */ public function serverAction(Request $request) { - if(!($extensionName = $request->get('extensionName'))) - { + if (!($extensionName = $request->get('extensionName'))) { throw $this->createNotFoundException('Unable to get config name'); } $servers = $this->container->getParameter('thrift.config.servers'); - if(!isset($servers[$extensionName])) - { + if (!isset($servers[$extensionName])) { throw $this->createNotFoundException(sprintf('Unknown config "%s"', $extensionName)); } diff --git a/DependencyInjection/Compiler/FactoryPass.php b/DependencyInjection/Compiler/FactoryPass.php index ac46340..7466e15 100755 --- a/DependencyInjection/Compiler/FactoryPass.php +++ b/DependencyInjection/Compiler/FactoryPass.php @@ -1,13 +1,23 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; + use Overblog\ThriftBundle\CacheWarmer\ThriftCompileCacheWarmer; use Overblog\ThriftBundle\Listener\ClassLoaderListener; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; /** - * Description of FactoryPass + * Description of FactoryPass. * * @author xavier */ @@ -18,11 +28,9 @@ class FactoryPass implements CompilerPassInterface * * @param ContainerBuilder $container * - * @return void - * * @api */ - function process(ContainerBuilder $container) + public function process(ContainerBuilder $container) { $cacheDir = $container->getParameter('kernel.cache_dir'); @@ -39,4 +47,3 @@ function process(ContainerBuilder $container) ClassLoaderListener::registerClassLoader($cacheDir); } } - diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 508d46e..5f3588e 100755 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -1,19 +1,28 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; /** - * This is the class that validates and merges configuration from your app/config files + * This is the class that validates and merges configuration from your app/config files. * * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} */ class Configuration implements ConfigurationInterface { /** - * {@inheritDoc} + * {@inheritdoc} */ public function getConfigTreeBuilder() { @@ -48,10 +57,10 @@ public function getConfigTreeBuilder() ->end() ->end() ->validate() - ->ifTrue(function($v){ + ->ifTrue(function ($v) { return empty($v['className']); }) - ->then(function($v){ + ->then(function ($v) { // If className is empty, definition is used $v['className'] = $v['definition']; @@ -95,10 +104,11 @@ public function getConfigTreeBuilder() ->end() //Server validation ->validate() - ->ifTrue( function($v) { - foreach($v['servers'] as $name => $server) - { - if(!isset($v['services'][$server['service']])) return true; + ->ifTrue(function ($v) { + foreach ($v['servers'] as $name => $server) { + if (!isset($v['services'][$server['service']])) { + return true; + } } return false; @@ -106,10 +116,11 @@ public function getConfigTreeBuilder() ->thenInvalid('Unknow service in servers configuration.') ->end() ->validate() - ->ifTrue( function($v) { - foreach($v['clients'] as $name => $client) - { - if(!isset($v['services'][$client['service']])) return true; + ->ifTrue(function ($v) { + foreach ($v['clients'] as $name => $client) { + if (!isset($v['services'][$client['service']])) { + return true; + } } return false; @@ -118,16 +129,14 @@ public function getConfigTreeBuilder() ->end() ->validate() ->always() - ->then( function($v) { + ->then(function ($v) { //Servers - foreach($v['servers'] as $name => $server) - { + foreach ($v['servers'] as $name => $server) { $v['servers'][$name]['service_config'] = $v['services'][$server['service']]; } //Clients - foreach($v['clients'] as $name => $client) - { + foreach ($v['clients'] as $name => $client) { $v['clients'][$name]['service_config'] = $v['services'][$client['service']]; } diff --git a/DependencyInjection/OverblogThriftExtension.php b/DependencyInjection/OverblogThriftExtension.php index c514d59..39e5849 100755 --- a/DependencyInjection/OverblogThriftExtension.php +++ b/DependencyInjection/OverblogThriftExtension.php @@ -1,23 +1,32 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\DependencyInjection; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; -use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\HttpKernel\DependencyInjection\Extension; /** - * This is the class that loads and manages your bundle configuration + * This is the class that loads and manages your bundle configuration. * * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} */ class OverblogThriftExtension extends Extension { /** - * {@inheritDoc} + * {@inheritdoc} */ public function load(array $configs, ContainerBuilder $container) { @@ -32,20 +41,20 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('thrift.config.servers', $config['servers']); // Register clients - foreach($config['clients'] as $name => $client) - { + foreach ($config['clients'] as $name => $client) { $this->loadClient($name, $client, $container, $config['testMode']); } } /** - * Create client service - * @param string $name - * @param array $client + * Create client service. + * + * @param string $name + * @param array $client * @param ContainerBuilder $container - * @param boolean $testMode + * @param bool $testMode */ - protected function loadClient($name, Array $client, ContainerBuilder $container, $testMode = false) + protected function loadClient($name, array $client, ContainerBuilder $container, $testMode = false) { $clientDef = new Definition( $container->getParameter( diff --git a/Exception/CompilerException.php b/Exception/CompilerException.php index 496e83d..e5030b6 100644 --- a/Exception/CompilerException.php +++ b/Exception/CompilerException.php @@ -1,10 +1,21 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Exception; /** - * This exception is throw when a compilation error is encountered + * This exception is throw when a compilation error is encountered. * * @author Xavier HAUSHERR */ -class CompilerException extends \Exception {} +class CompilerException extends \Exception +{ +} diff --git a/Exception/ConfigurationException.php b/Exception/ConfigurationException.php index 8c4f047..2715d03 100644 --- a/Exception/ConfigurationException.php +++ b/Exception/ConfigurationException.php @@ -1,15 +1,26 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Exception; /** - * Configuration Exception + * Configuration Exception. + * * @author Xavier HAUSHERR */ - -class ConfigurationException extends \Exception { - +class ConfigurationException extends \Exception +{ /** - * Create exception and set message + * Create exception and set message. + * * @param string $message */ public function __construct($message) @@ -18,12 +29,12 @@ public function __construct($message) } /** - * Return the message - * @return String + * Return the message. + * + * @return string */ public function __toString() { return $this->message; } - -} \ No newline at end of file +} diff --git a/Factory/ThriftFactory.php b/Factory/ThriftFactory.php index 8332cd8..d9e6368 100755 --- a/Factory/ThriftFactory.php +++ b/Factory/ThriftFactory.php @@ -1,51 +1,59 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Factory; /** - * Thrift factory + * Thrift factory. * * @author Xavier HAUSHERR */ - class ThriftFactory { protected $services; /** - * Inject dependencies + * Inject dependencies. + * * @param array $services */ - public function __construct(Array $services) + public function __construct(array $services) { $this->services = $services; } /** - * Return an instance of a Thrift Model Class + * Return an instance of a Thrift Model Class. * * @param string $classe - * @param mixed $param - * @return Object + * @param mixed $param + * + * @return object */ public function getInstance($classe, $param = null) { - if(is_null($param)) - { + if (is_null($param)) { return new $classe(); - } - else - { + } else { return new $classe($param); } } /** - * Return a processor instance + * Return a processor instance. * * @param string $service - * @param mixed $handler - * @return Object + * @param mixed $handler + * + * @return object */ public function getProcessorInstance($service, $handler) { @@ -55,11 +63,12 @@ public function getProcessorInstance($service, $handler) } /** - * Return a client instance - * - * @param string $service + * Return a client instance. + * + * @param string $service * @param Thrift\Protocol\TProtocol $protocol - * @return Object + * + * @return object */ public function getClientInstance($service, $protocol) { @@ -67,4 +76,4 @@ public function getClientInstance($service, $protocol) return new $classe($protocol); } -} \ No newline at end of file +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7f9b2d6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Overblog + +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. diff --git a/Listener/ClassLoaderListener.php b/Listener/ClassLoaderListener.php index 41843c3..0f9a78c 100755 --- a/Listener/ClassLoaderListener.php +++ b/Listener/ClassLoaderListener.php @@ -1,29 +1,38 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Listener; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; -use Symfony\Component\HttpKernel\HttpKernelInterface; +use Overblog\ThriftBundle\CacheWarmer\ThriftCompileCacheWarmer; use Symfony\Component\ClassLoader\MapClassLoader; use Symfony\Component\Console\Event\ConsoleCommandEvent; -use Overblog\ThriftBundle\CacheWarmer\ThriftCompileCacheWarmer; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\HttpKernelInterface; /** - * Inject Class Loader + * Inject Class Loader. * * @author xavier */ class ClassLoaderListener { /** - * SF Cache Dir + * SF Cache Dir. * * @var string */ protected $cacheDir; /** - * Inject Env + * Inject Env. * * @param string $cacheDir */ @@ -33,15 +42,14 @@ public function __construct($cacheDir) } /** - * Start Event of controller + * Start Event of controller. * * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { // Loader must be loaded only in master Request - if($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) - { + if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) { return; } @@ -49,7 +57,7 @@ public function onKernelRequest(GetResponseEvent $event) } /** - * Start Event of Command + * Start Event of Command. * * @param ConsoleCommandEvent $event */ @@ -59,7 +67,7 @@ public function onConsoleCommand(ConsoleCommandEvent $event) } /** - * Register Class Loader + * Register Class Loader. * * @param string $cacheDir */ diff --git a/OverblogThriftBundle.php b/OverblogThriftBundle.php index 4c668f9..8b8642a 100755 --- a/OverblogThriftBundle.php +++ b/OverblogThriftBundle.php @@ -1,16 +1,25 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle; -use Symfony\Component\HttpKernel\Bundle\Bundle; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Overblog\ThriftBundle\DependencyInjection\Compiler\FactoryPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpKernel\Bundle\Bundle; /** - * Overblog Thrift Bundle + * Overblog Thrift Bundle. + * * @author Xavier HAUSHERR */ - class OverblogThriftBundle extends Bundle { /** diff --git a/README.md b/README.md index 11ffe92..8c2f95e 100755 --- a/README.md +++ b/README.md @@ -175,3 +175,16 @@ $service->name = 'Name 1'; $id = $client->execMethod($service); ``` + +Contribute +---------- + +Tests: + +Install [phpunit](https://phpunit.de/manual/current/en/installation.html). + +In the bundle directory: + +```bash +phpunit +``` diff --git a/Routing/ThriftRoutingLoader.php b/Routing/ThriftRoutingLoader.php index 8b0f74c..45f455f 100644 --- a/Routing/ThriftRoutingLoader.php +++ b/Routing/ThriftRoutingLoader.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Routing; use Symfony\Component\Config\Loader\Loader; @@ -8,7 +17,6 @@ class ThriftRoutingLoader extends Loader { - protected $services; public function __construct($services) @@ -19,8 +27,8 @@ public function __construct($services) /** * Loads a resource. * - * @param mixed $resource The resource - * @param string|null $type The resource type or null if unknown + * @param mixed $resource The resource + * @param string|null $type The resource type or null if unknown * * @throws \Exception If something went wrong */ @@ -30,23 +38,24 @@ public function load($resource, $type = null) foreach ($this->services as $path => $service) { $route = new Route( '/'.$path, - array('_controller' => 'ThriftBundle:Thrift:server', 'extensionName' => $path), - array(), - array(), + ['_controller' => 'ThriftBundle:Thrift:server', 'extensionName' => $path], + [], + [], null, - array(), - array('post') + [], + ['post'] ); - $coll->add('thrift.' . $service['service'], $route); + $coll->add('thrift.'.$service['service'], $route); } + return $coll; } /** * Returns whether this class supports the given resource. * - * @param mixed $resource A resource - * @param string|null $type The resource type or null if unknown + * @param mixed $resource A resource + * @param string|null $type The resource type or null if unknown * * @return bool True if this class supports the given resource, false otherwise */ diff --git a/Server/HttpServer.php b/Server/HttpServer.php index eb2911f..982b6c8 100644 --- a/Server/HttpServer.php +++ b/Server/HttpServer.php @@ -1,21 +1,28 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ -use Overblog\ThriftBundle\Server\Server; +namespace Overblog\ThriftBundle\Server; use Thrift\Transport\TBufferedTransport; use Thrift\Transport\TPhpStream; /** - * HTTP Server class + * HTTP Server class. + * * @author Xavier HAUSHERR */ - class HttpServer extends Server { /** - * Run server + * Run server. */ public function run() { @@ -26,4 +33,4 @@ public function run() $this->processor->process($protocol, $protocol); $transport->close(); } -} \ No newline at end of file +} diff --git a/Server/Server.php b/Server/Server.php index 9c0b1b8..d7554ab 100644 --- a/Server/Server.php +++ b/Server/Server.php @@ -1,38 +1,49 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Server; /** - * Abstract class to create a server + * Abstract class to create a server. + * * @author Xavier HAUSHERR */ - abstract class Server { /** - * Thrift Processor + * Thrift Processor. */ protected $processor; /** - * Configuration + * Configuration. + * * @var array */ protected $config; /** - * Load dependencies + * Load dependencies. + * * @param mixed $processor * @param array $config */ - public function __construct($processor, Array $config) + public function __construct($processor, array $config) { $this->processor = $processor; $this->config = $config; } /** - * Return thrif header + * Return thrift header. */ public function getHeader() { @@ -40,7 +51,7 @@ public function getHeader() } /** - * Run the server + * Run the server. */ abstract public function run(); -} \ No newline at end of file +} diff --git a/Server/SocketServer.php b/Server/SocketServer.php index 15b1e48..c9093b3 100644 --- a/Server/SocketServer.php +++ b/Server/SocketServer.php @@ -1,25 +1,32 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ -use Overblog\ThriftBundle\Server\Server; +namespace Overblog\ThriftBundle\Server; -use Thrift\Server\TServerSocket; -use Thrift\Factory\TTransportFactory; use Thrift\Factory\TBinaryProtocolFactory; +use Thrift\Factory\TTransportFactory; +use Thrift\Server\TServerSocket; /** - * Socket Server class + * Socket Server class. + * * @author Xavier HAUSHERR */ - class SocketServer extends Server { /** - * Run socket server + * Run socket server. * * @param string $host - * @param int $port + * @param int $port */ public function run($host = 'localhost', $port = 9090) { @@ -28,7 +35,7 @@ public function run($host = 'localhost', $port = 9090) $outputProtocolFactory = $inputProtocolFactory = new TBinaryProtocolFactory(); // Do we use fork ? - $fork = 'Thrift\\Server\\' . ($this->config['fork'] ? 'TForkingServer' : 'TSimpleServer'); + $fork = 'Thrift\\Server\\'.($this->config['fork'] ? 'TForkingServer' : 'TSimpleServer'); $server = new $fork( $this->processor, @@ -41,4 +48,4 @@ public function run($host = 'localhost', $port = 9090) $server->serve(); } -} \ No newline at end of file +} diff --git a/Tests/Client/TestHandler.php b/Tests/Client/TestHandler.php index 36642ac..87e7a23 100644 --- a/Tests/Client/TestHandler.php +++ b/Tests/Client/TestHandler.php @@ -1,9 +1,18 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Tests\Client; -use ThriftModel\Test\Test; use Overblog\ThriftBundle\Factory\ThriftFactory; +use ThriftModel\Test\Test; class TestHandler { @@ -16,25 +25,23 @@ public function __construct(ThriftFactory $factory) public function ping() { - } public function get($id) { - if($id == -1) - { - $e = $this->factory->getInstance('ThriftModel\Test\InvalidValueException', array( + if ($id == -1) { + $e = $this->factory->getInstance('ThriftModel\Test\InvalidValueException', [ 'error_code' => 100, - 'error_msg' => 'ERROR' - )); + 'error_msg' => 'ERROR', + ]); throw $e; } - $test = $this->factory->getInstance('ThriftModel\Test\Test', array( + $test = $this->factory->getInstance('ThriftModel\Test\Test', [ 'id' => $id, - 'content' => 'TEST' - )); + 'content' => 'TEST', + ]); return $test; } @@ -47,21 +54,20 @@ public function getList($id) $test1->content = 'TEST2'; - return array($test, $test1); + return [$test, $test1]; } public function create($test) { - if(empty($test->content)) - { - $e = $this->factory->getInstance('ThriftModel\Test\InvalidValueException', array( + if (empty($test->content)) { + $e = $this->factory->getInstance('ThriftModel\Test\InvalidValueException', [ 'error_code' => 100, - 'error_msg' => 'ERROR' - )); + 'error_msg' => 'ERROR', + ]); throw $e; } return true; } -} \ No newline at end of file +} diff --git a/Tests/Client/ThriftClientTest.php b/Tests/Client/ThriftClientTest.php index 67b4b21..f98d81d 100755 --- a/Tests/Client/ThriftClientTest.php +++ b/Tests/Client/ThriftClientTest.php @@ -1,18 +1,26 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Tests\Client; -use Overblog\ThriftBundle\Tests\ThriftBundleTestCase; -use Overblog\ThriftBundle\Factory\ThriftFactory; use Overblog\ThriftBundle\Client\ThriftClient; -use Overblog\ThriftBundle\Tests\Client\TestHandler; - -use Thrift\Server\TServerSocket; -use Thrift\Factory\TTransportFactory; +use Overblog\ThriftBundle\Factory\ThriftFactory; +use Overblog\ThriftBundle\Tests\ThriftBundleTestCase; use Thrift\Factory\TBinaryProtocolFactory; +use Thrift\Factory\TTransportFactory; +use Thrift\Server\TServerSocket; use Thrift\Server\TSimpleServer; /** - * UNIT TEST + * UNIT TEST. * * @author Xavier HAUSHERR */ @@ -25,33 +33,33 @@ protected function setUp() parent::setUp(); parent::compile(); - $this->factory = new ThriftFactory(array( - 'test' => array( + $this->factory = new ThriftFactory([ + 'test' => [ 'definition' => 'Test', 'className' => 'TestService', - 'namespace' => 'ThriftModel\Test' - ) - )); + 'namespace' => 'ThriftModel\Test', + ], + ]); } public function testHttpClient() { - $thriftClient = new ThriftClient($this->factory, array( + $thriftClient = new ThriftClient($this->factory, [ 'service' => 'test', 'type' => 'http', - 'hosts' => array( - 'test' => array( + 'hosts' => [ + 'test' => [ 'host' => 'localhost/thrift', - 'port' => 80 - ) - ), - 'service_config' => array( + 'port' => 80, + ], + ], + 'service_config' => [ 'definition' => 'Test', 'className' => 'TestService', 'namespace' => 'ThriftModel\Test', - 'protocol' => 'Thrift\\Protocol\\TBinaryProtocolAccelerated' - ) - )); + 'protocol' => 'Thrift\\Protocol\\TBinaryProtocolAccelerated', + ], + ]); $this->assertInstanceOf('ThriftModel\Test\TestServiceClient', $thriftClient->getClient()); @@ -70,23 +78,23 @@ public function testHttpClient() protected function createSocketServer() { - return new ThriftClient($this->factory, array( + return new ThriftClient($this->factory, [ 'service' => 'test', 'type' => 'socket', - 'hosts' => array( - 'test' => array( + 'hosts' => [ + 'test' => [ 'host' => 'localhost', 'port' => 9090, - 'recvTimeout' => 1000 - ) - ), - 'service_config' => array( + 'recvTimeout' => 1000, + ], + ], + 'service_config' => [ 'definition' => 'Test', 'className' => 'Test', 'namespace' => 'ThriftModel\Test', - 'protocol' => 'Thrift\\Protocol\\TBinaryProtocolAccelerated' - ) - )); + 'protocol' => 'Thrift\\Protocol\\TBinaryProtocolAccelerated', + ], + ]); } public function testSocketClient__NoServer() @@ -134,8 +142,7 @@ public function testSocketClient() $pid = pcntl_fork(); - if ($pid > 0) - { + if ($pid > 0) { //Wait for the server to launch sleep(2); @@ -152,12 +159,9 @@ public function testSocketClient() $this->assertEquals('TEST', $test->content); // Wrong ID return Exception - try - { - $response = $client->get(-1);; - } - catch(\Exception $e) - { + try { + $response = $client->get(-1); + } catch (\Exception $e) { $this->assertInstanceOf('ThriftModel\Test\InvalidValueException', $e); } @@ -172,41 +176,33 @@ public function testSocketClient() $this->assertEquals('TEST2', $test[1]->content); // Wrong ID return Exception - try - { - $response = $client->getList(-1);; - } - catch(\Exception $e) - { + try { + $response = $client->getList(-1); + } catch (\Exception $e) { $this->assertInstanceOf('ThriftModel\Test\InvalidValueException', $e); } //Create test - $testObject = $this->factory->getInstance('ThriftModel\Test\Test', array( - 'content' => 'OK' - )); + $testObject = $this->factory->getInstance('ThriftModel\Test\Test', [ + 'content' => 'OK', + ]); $test = $client->create($testObject); - $this->assertTrue((bool)$test); + $this->assertTrue((bool) $test); $testObject->content = ''; // Wrong ID return Exception - try - { - $response = $client->create($testObject);; - } - catch(\Exception $e) - { + try { + $response = $client->create($testObject); + } catch (\Exception $e) { $this->assertInstanceOf('ThriftModel\Test\InvalidValueException', $e); } posix_kill($pid, SIGTERM); pcntl_wait($status, WNOHANG); - } - elseif ($pid === 0) - { + } elseif ($pid === 0) { $server->serve(); $transport->close(); @@ -218,26 +214,26 @@ public function testSocketClient() public function testMultiSocketClient() { - $thriftClient = new ThriftClient($this->factory, array( + $thriftClient = new ThriftClient($this->factory, [ 'service' => 'test', 'type' => 'socket', - 'hosts' => array( - 'test' => array( + 'hosts' => [ + 'test' => [ 'host' => 'localhost', - 'port' => 9090 - ), - 'test2' => array( + 'port' => 9090, + ], + 'test2' => [ 'host' => 'localhost', - 'port' => 9091 - ) - ), - 'service_config' => array( + 'port' => 9091, + ], + ], + 'service_config' => [ 'definition' => 'Test', 'className' => 'TestService', 'namespace' => 'ThriftModel\Test', - 'protocol' => 'Thrift\\Protocol\\TBinaryProtocolAccelerated' - ) - )); + 'protocol' => 'Thrift\\Protocol\\TBinaryProtocolAccelerated', + ], + ]); $this->assertInstanceOf( 'ThriftModel\Test\Test', @@ -245,12 +241,9 @@ public function testMultiSocketClient() ); // Server doesn't exists - try - { + try { $thriftClient->getClient(); - } - catch(\Exception $e) - { + } catch (\Exception $e) { $this->assertInstanceOf('Thrift\Exception\TException', $e); } } diff --git a/Tests/Command/AppKernelMock.php b/Tests/Command/AppKernelMock.php index c79269b..a6ab0d4 100644 --- a/Tests/Command/AppKernelMock.php +++ b/Tests/Command/AppKernelMock.php @@ -1,23 +1,31 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + /** - * UNIT TEST + * UNIT TEST. * * @author Xavier HAUSHERR */ namespace Overblog\ThriftBundle\Tests\Command; -use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\HttpKernel\Kernel; class AppKernelMock extends Kernel { public function registerBundles() { - } public function registerContainerConfiguration(LoaderInterface $loader) { - } -} \ No newline at end of file +} diff --git a/Tests/Command/CompileCommandTest.php b/Tests/Command/CompileCommandTest.php index ebecb88..cfe711f 100644 --- a/Tests/Command/CompileCommandTest.php +++ b/Tests/Command/CompileCommandTest.php @@ -1,19 +1,26 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + /** - * UNIT TEST + * UNIT TEST. * * @author Xavier HAUSHERR */ namespace Overblog\ThriftBundle\Tests\Compiler; -use Symfony\Component\Console\Tester\CommandTester; -use Symfony\Bundle\FrameworkBundle\Console\Application; - -use Overblog\ThriftBundle\Tests\ThriftBundleTestCase; -use Overblog\ThriftBundle\Tests\Command\AppKernelMock; -use Overblog\ThriftBundle\Compiler\ThriftCompiler; - use Overblog\ThriftBundle\Command\CompileCommand; +use Overblog\ThriftBundle\Tests\Command\AppKernelMock; +use Overblog\ThriftBundle\Tests\ThriftBundleTestCase; +use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; class CompileCommandTest extends ThriftBundleTestCase { @@ -21,7 +28,7 @@ class CompileCommandTest extends ThriftBundleTestCase protected function setUp() { -// parent::setUp(); + // parent::setUp(); // // $kernel = new AppKernelMock('test', false); // $this->application = new Application($kernel); @@ -30,7 +37,7 @@ protected function setUp() public function testExecute() { -// $command = $this->application->find('thrift:compile'); + // $command = $this->application->find('thrift:compile'); // $commandTester = new CommandTester($command); // $commandTester->execute(array( // 'command' => $command->getName(), @@ -44,4 +51,4 @@ public function testExecute() // // $this->assertRegExp('/.../', $commandTester->getDisplay()); } -} \ No newline at end of file +} diff --git a/Tests/Command/ServerTest.php b/Tests/Command/ServerTest.php index 64f3a92..0347745 100644 --- a/Tests/Command/ServerTest.php +++ b/Tests/Command/ServerTest.php @@ -1,22 +1,28 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + /** - * UNIT TEST + * UNIT TEST. * * @author Xavier HAUSHERR */ namespace Overblog\ThriftBundle\Tests\Command; - class ServerTest extends \PHPUnit_Framework_TestCase { - protected function setUp() { - } public function testServer() { - } -} \ No newline at end of file +} diff --git a/Tests/Compiler/CompilerTest.php b/Tests/Compiler/CompilerTest.php index 5240a63..66e3729 100644 --- a/Tests/Compiler/CompilerTest.php +++ b/Tests/Compiler/CompilerTest.php @@ -1,11 +1,21 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Tests\Compiler; -use Overblog\ThriftBundle\Tests\ThriftBundleTestCase; use Overblog\ThriftBundle\Compiler\ThriftCompiler; +use Overblog\ThriftBundle\Tests\ThriftBundleTestCase; /** - * UNIT TEST + * UNIT TEST. * * @author Xavier HAUSHERR */ @@ -29,12 +39,12 @@ public function testCompile() // Check if the return is correct $this->assertEquals($compiler->getLastOutput(), - array( + [ sprintf('Scanning %s for includes', realpath($this->definitionPath)), sprintf('Parsing %s for types', realpath($this->definitionPath)), sprintf('Program: %s', realpath($this->definitionPath)), - sprintf('Generating "php:oop,nsglobal=%s"', $this->namespace) - ) + sprintf('Generating "php:oop,nsglobal=%s"', $this->namespace), + ] ); // Now compile with server @@ -42,20 +52,20 @@ public function testCompile() // Check if the return is correct $this->assertEquals($compiler->getLastOutput(), - array( + [ sprintf('Scanning %s for includes', realpath($this->definitionPath)), sprintf('Parsing %s for types', realpath($this->definitionPath)), sprintf('Program: %s', realpath($this->definitionPath)), - sprintf('Generating "php:oop,nsglobal=%s,server"', $this->namespace) - ) + sprintf('Generating "php:oop,nsglobal=%s,server"', $this->namespace), + ] ); // Unknow definition $this->setExpectedException('Overblog\ThriftBundle\Exception\ConfigurationException', - sprintf('Unable to find Thrift definition at path "%s"', $this->definitionPath . 'UNKNOWN') - ); + sprintf('Unable to find Thrift definition at path "%s"', $this->definitionPath.'UNKNOWN') + ); - $compiler->compile($this->definitionPath . 'UNKNOWN', true); + $compiler->compile($this->definitionPath.'UNKNOWN', true); } public function testExec() @@ -64,8 +74,8 @@ public function testExec() // Bad exec path $this->setExpectedException('Overblog\ThriftBundle\Exception\ConfigurationException', - 'Unable to find Thrift executable' - ); + 'Unable to find Thrift executable' + ); $compiler->setExecPath(__DIR__); } diff --git a/Tests/Factory/ThriftFactoryTest.php b/Tests/Factory/ThriftFactoryTest.php index 888ebb9..38161e5 100755 --- a/Tests/Factory/ThriftFactoryTest.php +++ b/Tests/Factory/ThriftFactoryTest.php @@ -1,11 +1,21 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Tests\Factory; -use Overblog\ThriftBundle\Tests\ThriftBundleTestCase; use Overblog\ThriftBundle\Factory\ThriftFactory; +use Overblog\ThriftBundle\Tests\ThriftBundleTestCase; /** - * UNIT TEST + * UNIT TEST. * * @author Xavier HAUSHERR */ @@ -19,13 +29,13 @@ protected function setUp() public function testFactory() { - $factory = new ThriftFactory(array( - 'test' => array( + $factory = new ThriftFactory([ + 'test' => [ 'definition' => 'Test', 'className' => 'TestService', - 'namespace' => 'ThriftModel\Test' - ) - )); + 'namespace' => 'ThriftModel\Test', + ], + ]); $this->assertInstanceOf( 'ThriftModel\Test\Test', @@ -34,7 +44,7 @@ public function testFactory() $this->assertInstanceOf( 'ThriftModel\Test\Test', - $factory->getInstance('ThriftModel\Test\Test', array()) + $factory->getInstance('ThriftModel\Test\Test', []) ); $this->assertInstanceOf( diff --git a/Tests/ThriftBundleTestCase.php b/Tests/ThriftBundleTestCase.php index ae52ea1..0b371a6 100755 --- a/Tests/ThriftBundleTestCase.php +++ b/Tests/ThriftBundleTestCase.php @@ -1,10 +1,19 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Overblog\ThriftBundle\Tests; use Overblog\ThriftBundle\Compiler\ThriftCompiler; -use Symfony\Component\ClassLoader\MapClassLoader; use Symfony\Component\ClassLoader\ClassMapGenerator; +use Symfony\Component\ClassLoader\MapClassLoader; class ThriftBundleTestCase extends \PHPUnit_Framework_TestCase { @@ -15,8 +24,8 @@ class ThriftBundleTestCase extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->modelPath = __DIR__ . '/thrift'; - $this->definitionPath = __DIR__ . '/ThriftDefinition/Test.thrift'; + $this->modelPath = __DIR__.'/thrift'; + $this->definitionPath = __DIR__.'/ThriftDefinition/Test.thrift'; } protected function compile() diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php index b27d9e5..ea02bf2 100644 --- a/Tests/bootstrap.php +++ b/Tests/bootstrap.php @@ -1,3 +1,12 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + $loader = require __DIR__.'/../vendor/autoload.php'; diff --git a/composer.json b/composer.json index 9b1e51a..3e14a85 100755 --- a/composer.json +++ b/composer.json @@ -10,17 +10,19 @@ "homepage": "http://www.over-blog.com" } ], - "require": { - "php": ">=5.3.0", - "apache/thrift": ">=0.9.1", - "symfony/symfony": "~2.3|~3.0" - }, "autoload": { "psr-4": {"Overblog\\ThriftBundle\\": ""}, "exclude-from-classmap": [ "/Tests/" ] }, - "require-dev": { - } + "config" : { + "sort-packages": true + }, + "require": { + "php": "^5.3.6|~7.0", + "apache/thrift": ">=0.9.1", + "symfony/symfony": "~2.3|~3.0" + }, + "require-dev": {} }