From a562bc661d0a20749f423ff727bcc9dd79ddf728 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 6 Dec 2018 21:05:11 +0100 Subject: [PATCH 1/3] Improved CacheFactory class --- module/Common/src/Factory/CacheFactory.php | 42 ++++++++++------ module/Core/src/Options/AppOptions.php | 58 ++++++---------------- 2 files changed, 42 insertions(+), 58 deletions(-) diff --git a/module/Common/src/Factory/CacheFactory.php b/module/Common/src/Factory/CacheFactory.php index bd0eea3f..2efde447 100644 --- a/module/Common/src/Factory/CacheFactory.php +++ b/module/Common/src/Factory/CacheFactory.php @@ -13,6 +13,7 @@ use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\Factory\FactoryInterface; use function Functional\contains; use function Shlinkio\Shlink\Common\env; +use function sys_get_temp_dir; class CacheFactory implements FactoryInterface { @@ -23,6 +24,7 @@ class CacheFactory implements FactoryInterface Cache\PhpFileCache::class, Cache\MemcachedCache::class, ]; + private const DEFAULT_MEMCACHED_PORT = 11211; /** * Create an object @@ -40,7 +42,7 @@ class CacheFactory implements FactoryInterface { $appOptions = $container->get(AppOptions::class); $adapter = $this->getAdapter($container); - $adapter->setNamespace($appOptions->__toString()); + $adapter->setNamespace((string) $appOptions); return $adapter; } @@ -65,25 +67,35 @@ class CacheFactory implements FactoryInterface return new $cacheConfig['adapter'](); case Cache\FilesystemCache::class: case Cache\PhpFileCache::class: - return new $cacheConfig['adapter']($cacheConfig['options']['dir']); + return new $cacheConfig['adapter']($cacheConfig['options']['dir'] ?? sys_get_temp_dir()); case Cache\MemcachedCache::class: - $memcached = new Memcached(); - $servers = $cacheConfig['options']['servers'] ?? []; - - foreach ($servers as $server) { - if (! isset($server['host'])) { - continue; - } - $port = isset($server['port']) ? (int) $server['port'] : 11211; - - $memcached->addServer($server['host'], $port); - } - $cache = new Cache\MemcachedCache(); - $cache->setMemcached($memcached); + $cache->setMemcached($this->buildMemcached($cacheConfig)); return $cache; default: return new Cache\ArrayCache(); } } + + private function buildMemcached(array $cacheConfig): Memcached + { + $memcached = new Memcached(); + $servers = $cacheConfig['options']['servers'] ?? []; + + foreach ($servers as $server) { + $this->addMemcachedServer($memcached, $server); + } + + return $memcached; + } + + private function addMemcachedServer(Memcached $memcached, array $server): void + { + if (! isset($server['host'])) { + return; + } + $port = (int) ($server['port'] ?? self::DEFAULT_MEMCACHED_PORT); + + $memcached->addServer($server['host'], $port); + } } diff --git a/module/Core/src/Options/AppOptions.php b/module/Core/src/Options/AppOptions.php index b448b8c0..341d5df5 100644 --- a/module/Core/src/Options/AppOptions.php +++ b/module/Core/src/Options/AppOptions.php @@ -15,69 +15,48 @@ class AppOptions extends AbstractOptions private $name = ''; /** @var string */ private $version = '1.0'; - /** @var string */ + /** + * @var string + * @deprecated + */ private $secretKey = ''; /** @var string|null */ private $disableTrackParam; - /** - * AppOptions constructor. - * @param array|null|\Traversable $options - */ - public function __construct($options = null) - { - parent::__construct($options); - } - - /** - * @return string - */ - public function getName() + public function getName(): string { return $this->name; } - /** - * @param string $name - * @return $this - */ - protected function setName($name) + protected function setName(string $name): self { $this->name = $name; return $this; } - /** - * @return string - */ - public function getVersion() + public function getVersion(): string { return $this->version; } - /** - * @param string $version - * @return $this - */ - protected function setVersion($version) + protected function setVersion(string $version): self { $this->version = $version; return $this; } /** - * @return mixed + * @deprecated */ - public function getSecretKey() + public function getSecretKey(): string { return $this->secretKey; } /** - * @param mixed $secretKey - * @return $this + * @deprecated */ - protected function setSecretKey($secretKey) + protected function setSecretKey(string $secretKey): self { $this->secretKey = $secretKey; return $this; @@ -86,25 +65,18 @@ class AppOptions extends AbstractOptions /** * @return string|null */ - public function getDisableTrackParam() + public function getDisableTrackParam(): ?string { return $this->disableTrackParam; } - /** - * @param string|null $disableTrackParam - * @return $this|self - */ - protected function setDisableTrackParam($disableTrackParam): self + protected function setDisableTrackParam(?string $disableTrackParam): self { $this->disableTrackParam = $disableTrackParam; return $this; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return sprintf('%s:v%s', $this->name, $this->version); } From 69dd9eb067b94eb192309b4a34b407f1a1e374e9 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 7 Dec 2018 02:41:06 +0100 Subject: [PATCH 2/3] Updated readme mentioning docker image --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9aff58a5..e49c1fd8 100644 --- a/README.md +++ b/README.md @@ -214,11 +214,9 @@ Right now, it does not import cached info (like website previews), but it will. ## Using a docker image -Currently there's no official docker image, but there's a work in progress alpha version you can find [here](https://hub.docker.com/r/shlinkio/shlink/). +Starting with version 1.15.0, an official docker image is provided. You can find the docs on how to use it [here](https://hub.docker.com/r/shlinkio/shlink/). -The idea will be that you can just generate a container using the image and provide predefined config files via volumes or CLI arguments, so that you get shlink up and running. - -Currently the image does not expose an entry point which let's you interact with shlink's CLI interface, nor allows configuration to be passed. +The idea is that you can just generate a container using the image and provide custom config via env vars. ## Using shlink From accda36a7b9a401a0fbc7f985bc7a9e1e56b765b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 7 Dec 2018 02:49:50 +0100 Subject: [PATCH 3/3] Updated default secret_key value --- config/autoload/app_options.global.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/autoload/app_options.global.php b/config/autoload/app_options.global.php index e280b4cf..9b363f1f 100644 --- a/config/autoload/app_options.global.php +++ b/config/autoload/app_options.global.php @@ -8,7 +8,7 @@ return [ 'app_options' => [ 'name' => 'Shlink', 'version' => '%SHLINK_VERSION%', - 'secret_key' => env('SECRET_KEY'), + 'secret_key' => env('SECRET_KEY', ''), 'disable_track_param' => null, ],