diff --git a/.travis.yml b/.travis.yml index 56baa16..9ce8006 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,20 +5,20 @@ php: - 5.5 - 5.6 - hhvm -services: - - memcached + before_script: - - "sudo apt-get install php5-memcached" - - "phpenv config-add travis.php.ini" - composer self-update - composer install - chmod -R 777 tests/storage + script: - mkdir -p build/logs - chmod -R 777 build/logs - phpunit + after_script: - php vendor/bin/coveralls -v + notifications: emails: - yuuki.takezawa@comnect.jp.net diff --git a/README.md b/README.md index 35e3aae..38e03e7 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,36 @@ $ php artisan ytake:smarty-optimize | --extension (-e) | specified smarty file extension(default: *.tpl*) | | --force | compiles template files found in views directory | +Template Caching +================== +**choose file, memcached, Redis** +(default file cache driver) + +テンプレートキャッシュドライバにファイルキャッシュ、memcached, Redisが選択できます +デフォルトは通常のファイルキャッシュになっています +```php +// smarty cache driver "file", "memcached", "redis" +'cache_driver' => 'file', + +// memcached servers +'memcached' => [ + [ + 'host' => '127.0.0.1', + 'port' => 11211, + 'weight' => 100 + ], +], + +// redis configure +'redis' => [ + [ + 'host' => '127.0.0.1', + 'port' => 6379, + 'database' => 0, + ], +], +``` + Usage ================== @@ -102,7 +132,7 @@ app/config配下のapp.phpのproviders配列に以下のnamespaceを追加して add providers ```php 'providers' => [ -'Ytake\LaravelSmarty\SmartyServiceProvider' + 'Ytake\LaravelSmarty\SmartyServiceProvider' ] ``` diff --git a/composer.json b/composer.json index cf3a209..37d45d8 100644 --- a/composer.json +++ b/composer.json @@ -18,17 +18,17 @@ "smarty/smarty": "3.*@stable" }, "require-dev": { - "predis/predis": "0.*", + "predis/predis": "0.*", "symfony/framework-bundle": "2.*", "symfony/console": "2.*", "phpunit/phpunit": "4.*", "mockery/mockery": "0.*", "satooshi/php-coveralls": "dev-master" }, - "suggest": { - "ext-memcached": "memcached Template Cache Driver", - "predis/predis": "Redis Template Cache Driver" - }, + "suggest": { + "ext-memcached": "memcached Template Cache Driver", + "predis/predis": "Redis Template Cache Driver" + }, "autoload": { "psr-4": { "Ytake\\LaravelSmarty\\": "src/Smarty" diff --git a/src/Smarty/Cache/Memcached.php b/src/Smarty/Cache/Memcached.php index c8527cc..9d0af02 100644 --- a/src/Smarty/Cache/Memcached.php +++ b/src/Smarty/Cache/Memcached.php @@ -27,12 +27,12 @@ public function __construct(MemcachedExtendion $memcached, array $servers) * @param array $servers * @return \Memcached */ - public function connection(MemcachedExtendion $memcached, array $servers) + protected function connection(MemcachedExtendion $memcached, array $servers) { foreach ($servers as $server) { - $memcached->addServer($server['host'], $server['port'], $server['weight']); + $memcached->addServer($server['host'], $server['port'], $server['weight']); } - return $memcached; + return $memcached; } diff --git a/src/Smarty/Cache/Redis.php b/src/Smarty/Cache/Redis.php index a39c23d..9cd1841 100644 --- a/src/Smarty/Cache/Redis.php +++ b/src/Smarty/Cache/Redis.php @@ -12,19 +12,19 @@ class Redis extends \Smarty_CacheResource_KeyValueStore { - /** @var Client */ + /** @var Client */ protected $redis; - /** - * @param array $servers - */ + /** + * @param array $servers + */ public function __construct(array $servers) { - if(count($servers) === 1) { - $this->redis = new Client($servers[0]); - } else { - $this->redis = new Client($servers); - } + if(count($servers) === 1) { + $this->redis = new Client($servers[0]); + } else { + $this->redis = new Client($servers); + } } /** @@ -42,9 +42,9 @@ protected function read(array $keys) $lookup[$_k] = $k; } $_res = []; - foreach($_keys as $key) { - $_res[$lookup[$key]] = $this->redis->get($key); - } + foreach($_keys as $key) { + $_res[$lookup[$key]] = $this->redis->get($key); + } return $_res; } @@ -58,7 +58,7 @@ protected function write(array $keys, $expire = null) { foreach ($keys as $k => $v) { $k = sha1($k); - $this->redis->setex($k, $expire, $v); + $this->redis->setex($k, $expire, $v); } return true; } diff --git a/src/Smarty/Cache/Storage.php b/src/Smarty/Cache/Storage.php index 97307b4..7482334 100644 --- a/src/Smarty/Cache/Storage.php +++ b/src/Smarty/Cache/Storage.php @@ -35,30 +35,30 @@ public function __construct(Smarty $smarty, Repository $repository) */ protected function cacheStorageManaged() { - $driver = $this->repository->get('laravel-smarty::cache_driver', 'file'); - if($driver !== 'file') { - $storage = $driver . "Storage"; - $this->smarty->registerCacheResource($driver, $this->$storage()); - } - $this->smarty->caching_type = $driver; + $driver = $this->repository->get('laravel-smarty::cache_driver', 'file'); + if($driver !== 'file') { + $storage = $driver . "Storage"; + $this->smarty->registerCacheResource($driver, $this->$storage()); + } + $this->smarty->caching_type = $driver; } - /** - * @return Redis - */ - protected function redisStorage() + /** + * @return Redis + */ + protected function redisStorage() { - return new Redis($this->repository->get('laravel-smarty::redis')); + return new Redis($this->repository->get('laravel-smarty::redis')); } - /** - * @return Memcached - */ - protected function memcachedStorage() + /** + * @return Memcached + */ + protected function memcachedStorage() { return new Memcached( - new \Memcached(), - $this->repository->get('laravel-smarty::memcached') + new \Memcached(), + $this->repository->get('laravel-smarty::memcached') ); } } diff --git a/src/Smarty/Console/CacheClearCommand.php b/src/Smarty/Console/CacheClearCommand.php index f8691a2..e52f9a3 100644 --- a/src/Smarty/Console/CacheClearCommand.php +++ b/src/Smarty/Console/CacheClearCommand.php @@ -25,24 +25,24 @@ public function __construct(Smarty $smarty) $this->smarty = $smarty; } - /** - * The console command name. - * @var string - */ - protected $name = 'ytake:smarty-clear-cache'; + /** + * The console command name. + * @var string + */ + protected $name = 'ytake:smarty-clear-cache'; - /** - * The console command description. - * @var string - */ - protected $description = 'Flush the smarty cache'; + /** + * The console command description. + * @var string + */ + protected $description = 'Flush the smarty cache'; - /** - * Execute the console command. - * @return void - */ - public function fire() - { + /** + * Execute the console command. + * @return void + */ + public function fire() + { // clear all cache if(is_null($this->option('file'))) { $this->smarty->clearAllCache($this->option('time')); @@ -56,7 +56,7 @@ public function fire() } $this->info('specified file was cache cleared!'); return; - } + } /** * Get the console command options. diff --git a/src/Smarty/Console/CompiledClearCommand.php b/src/Smarty/Console/CompiledClearCommand.php index 5bdc739..1998060 100644 --- a/src/Smarty/Console/CompiledClearCommand.php +++ b/src/Smarty/Console/CompiledClearCommand.php @@ -26,30 +26,30 @@ public function __construct(Smarty $smarty) $this->smarty = $smarty; } - /** - * The console command name. - * @var string - */ - protected $name = 'ytake:smarty-clear-compiled'; + /** + * The console command name. + * @var string + */ + protected $name = 'ytake:smarty-clear-compiled'; - /** - * The console command description. - * @var string - */ - protected $description = 'Remove the compiled smarty file'; + /** + * The console command description. + * @var string + */ + protected $description = 'Remove the compiled smarty file'; - /** - * Execute the console command. - * @return void - */ - public function fire() - { + /** + * Execute the console command. + * @return void + */ + public function fire() + { if($this->smarty->clearCompiledTemplate($this->option('file'), $this->option('compile_id'))) { $this->info('done.'); return; } return; - } + } /** * Get the console command options. diff --git a/src/Smarty/Console/CompiledCommand.php b/src/Smarty/Console/CompiledCommand.php index f7f318a..c27181e 100644 --- a/src/Smarty/Console/CompiledCommand.php +++ b/src/Smarty/Console/CompiledCommand.php @@ -32,24 +32,24 @@ public function __construct(Smarty $smarty, Repository $config) $this->config = $config; } - /** - * The console command name. - * @var string - */ - protected $name = 'ytake:smarty-optimize'; + /** + * The console command name. + * @var string + */ + protected $name = 'ytake:smarty-optimize'; - /** - * The console command description. - * @var string - */ - protected $description = 'compiles all known templates'; + /** + * The console command description. + * @var string + */ + protected $description = 'compiles all known templates'; - /** - * Execute the console command. - * @return void - */ - public function fire() - { + /** + * Execute the console command. + * @return void + */ + public function fire() + { $configureFileExtension = $this->config->get('laravel-smarty::extension', 'tpl'); $fileExtension = (is_null($this->option('extension'))) ? $configureFileExtension : $this->option('extension'); ob_start(); @@ -59,7 +59,7 @@ public function fire() $this->info("{$compileFiles} template files recompiled"); $this->comment(str_replace("
", "\n", trim($contents))); return; - } + } /** * Get the console command options. diff --git a/src/Smarty/Console/PackageInfoCommand.php b/src/Smarty/Console/PackageInfoCommand.php index 701cb32..bf0e090 100644 --- a/src/Smarty/Console/PackageInfoCommand.php +++ b/src/Smarty/Console/PackageInfoCommand.php @@ -14,26 +14,26 @@ class PackageInfoCommand extends Command { - /** - * The console command name. - * @var string - */ - protected $name = 'ytake:smarty-package-info'; + /** + * The console command name. + * @var string + */ + protected $name = 'ytake:smarty-package-info'; - /** - * The console command description. - * @var string - */ - protected $description = 'information about ytake/laravel-smarty'; + /** + * The console command description. + * @var string + */ + protected $description = 'information about ytake/laravel-smarty'; - /** - * Execute the console command. - * @return void - */ - public function fire() - { - $this->line('Smarty version ' . Smarty::SMARTY_VERSION . ''); - $this->line('ytake/laravel-smarty version ' . SmartyManager::VERSION . ''); - } + /** + * Execute the console command. + * @return void + */ + public function fire() + { + $this->line('Smarty version ' . Smarty::SMARTY_VERSION . ''); + $this->line('ytake/laravel-smarty version ' . SmartyManager::VERSION . ''); + } } diff --git a/src/Smarty/SmartyServiceProvider.php b/src/Smarty/SmartyServiceProvider.php index 70f8b18..0dc9646 100644 --- a/src/Smarty/SmartyServiceProvider.php +++ b/src/Smarty/SmartyServiceProvider.php @@ -26,8 +26,8 @@ class SmartyServiceProvider extends ServiceProvider public function boot() { $this->package('ytake/laravel-smarty'); - // - $this->registerCacheStorage(); + // register template cache driver + $this->registerCacheStorage(); // register commands $this->registerCommands(); } @@ -114,9 +114,9 @@ function () { ); } - /** - * @return Storage - */ + /** + * @return Storage + */ protected function registerCacheStorage() { return new Storage($this->app['view']->getSmarty(), $this->app['config']); diff --git a/src/config/config.php b/src/config/config.php index e482390..ea499eb 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -30,19 +30,19 @@ // path info 'template_path' => base_path() . '/app/views', - 'cache_path' => base_path() . '/app/storage/smarty/cache', + 'cache_path' => base_path() . '/app/storage/smarty/cache', - 'compile_path' => base_path() . '/app/storage/smarty/compile', + 'compile_path' => base_path() . '/app/storage/smarty/compile', - 'plugins_paths' => [ + 'plugins_paths' => [ base_path() . '/app/views/smarty/plugins', ], - 'config_paths' => [ + 'config_paths' => [ base_path() . '/app/config/smarty', ], - // production false + // production false 'force_compile' => true, // smarty cache driver "file", "memcached", "redis" diff --git a/tests/Cache/StorageTest.php b/tests/Cache/StorageTest.php index aad539f..32e0e0e 100644 --- a/tests/Cache/StorageTest.php +++ b/tests/Cache/StorageTest.php @@ -1,5 +1,5 @@ assertInstanceOf("Ytake\LaravelSmarty\Cache\Storage", $this->storage); @@ -32,8 +37,11 @@ public function testRedisDriver() public function testMemcachedDriver() { - $reflection = $this->getProtectMethod($this->storage, 'memcachedStorage'); - $this->assertInstanceOf("Ytake\LaravelSmarty\Cache\Memcached", $reflection->invoke($this->storage)); + $storageMock = m::mock($this->storage); + $storageMock->makePartial()->shouldAllowMockingProtectedMethods(); + $storageMock->shouldReceive("memcachedStorage")->andReturn("Ytake\LaravelSmarty\Cache\Memcached"); + $reflection = $this->getProtectMethod($storageMock, 'memcachedStorage'); + $this->assertEquals("Ytake\LaravelSmarty\Cache\Memcached", $reflection->invoke($storageMock)); } /** diff --git a/tests/config/config.php b/tests/config/config.php index e4b0c22..9d346c3 100644 --- a/tests/config/config.php +++ b/tests/config/config.php @@ -1,47 +1,47 @@ 'tpl', - // - 'debugging' => false, - // use cache - 'caching' => false, - // - 'cache_lifetime' => 120, - // - 'compile_check' => false, - // delimiters - // default "{$smarty}" - 'left_delimiter' => '{', - 'right_delimiter' => '}', - // path info - 'template_path' => PATH . '/views', - 'cache_path' => PATH . '/storage/smarty/cache', - 'compile_path' => PATH . '/storage/smarty/compile', - 'plugins_paths' => [ + // smarty file extension + 'extension' => 'tpl', + // + 'debugging' => false, + // use cache + 'caching' => false, + // + 'cache_lifetime' => 120, + // + 'compile_check' => false, + // delimiters + // default "{$smarty}" + 'left_delimiter' => '{', + 'right_delimiter' => '}', + // path info + 'template_path' => PATH . '/views', + 'cache_path' => PATH . '/storage/smarty/cache', + 'compile_path' => PATH . '/storage/smarty/compile', + 'plugins_paths' => [ PATH . '/views/plugins', - ], + ], 'force_compile' => true, - // smarty cache driver "file", "memcached", "redis" - 'cache_driver' => 'file', + // smarty cache driver "file", "memcached", "redis" + 'cache_driver' => 'file', - // memcached servers - 'memcached' => [ - [ - 'host' => '127.0.0.1', - 'port' => 11211, - 'weight' => 100 - ], - ], + // memcached servers + 'memcached' => [ + [ + 'host' => '127.0.0.1', + 'port' => 11211, + 'weight' => 100 + ], + ], - // redis configure - 'redis' => [ - [ - 'host' => '127.0.0.1', - 'port' => 6379, - 'database' => 0, - ], - ], + // redis configure + 'redis' => [ + [ + 'host' => '127.0.0.1', + 'port' => 6379, + 'database' => 0, + ], + ], ]; diff --git a/travis.php.ini b/travis.php.ini deleted file mode 100644 index 11a613a..0000000 --- a/travis.php.ini +++ /dev/null @@ -1 +0,0 @@ -extension=memcached.so \ No newline at end of file