From a2e2f1b1943d10d27288e6e0973b9aaf1f105928 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Wed, 24 Sep 2014 13:04:37 +0000 Subject: [PATCH] Use Stash as a cache system --- .gitignore | 4 ++ src/composer.json | 3 +- src/gulpfile.js | 38 ++++++++++++++--- src/mibew/README.txt | 9 ++-- src/mibew/cache/.keep | 0 src/mibew/libs/classes/Mibew/Application.php | 8 +++- .../Mibew/Cache/CacheAwareInterface.php | 42 +++++++++++++++++++ .../Mibew/Controller/AbstractController.php | 26 +++++++++++- .../Mibew/Controller/ControllerResolver.php | 37 ++++++++++++++-- 9 files changed, 151 insertions(+), 16 deletions(-) create mode 100644 src/mibew/cache/.keep create mode 100644 src/mibew/libs/classes/Mibew/Cache/CacheAwareInterface.php diff --git a/.gitignore b/.gitignore index 25cb1ebd..04d31eb3 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,10 @@ src/tests/server_side/mibew/libs/config.php src/mibew/files/avatar/* !src/mibew/files/avatar/.keep +# Do not index cache files +src/mibew/cache/* +!src/mibew/cache/.keep + # Do not index plugins src/mibew/plugins/* !src/mibew/plugins/.keep diff --git a/src/composer.json b/src/composer.json index 5fa62330..dab27a7b 100644 --- a/src/composer.json +++ b/src/composer.json @@ -23,7 +23,8 @@ "symfony/routing": "2.5.*", "symfony/config": "2.5.*", "symfony/yaml": "2.5.*", - "symfony/translation": "2.5.*" + "symfony/translation": "2.5.*", + "tedivm/stash": "0.12.*" }, "require-dev": { "squizlabs/php_codesniffer": "1.*" diff --git a/src/gulpfile.js b/src/gulpfile.js index 9f28ded0..fc83971a 100644 --- a/src/gulpfile.js +++ b/src/gulpfile.js @@ -28,6 +28,7 @@ var config = { mibewPath: 'mibew', phpVendorPath: 'mibew/vendor', pluginsPath: 'mibew/plugins', + cachePath: 'mibew/cache', jsPath: 'mibew/js', chatStylesPath: 'mibew/styles/dialogs', pageStylesPath: 'mibew/styles/pages', @@ -43,8 +44,15 @@ gulp.task('phpcs', ['composer-install-dev'], function() { return gulp.src([ config.mibewPath + '/**/*.php', '!' + config.phpVendorPath + '/**/*.*', - '!' + config.pluginsPath + '/**/*.*' - ]) + '!' + config.pluginsPath + '/**/*.*', + '!' + config.cachePath + '/**/*.*' + ], { + // Content of the cache directory is readable only for webserver. Thus + // we must to set "strict" option to false to prevent "EACCES" errors. + // At the same we need to see all errors that take place. + strict: false, + silent: false + }) .pipe(phpcs({ bin: config.phpVendorPath + '/bin/phpcs', standard: 'PSR2', @@ -167,8 +175,15 @@ gulp.task('generate-pot', function() { gulp.src([ config.mibewPath + '/**/*.php', '!' + config.phpVendorPath + '/**/*.*', - '!' + config.pluginsPath + '/**/*.*' - ]) + '!' + config.pluginsPath + '/**/*.*', + '!' + config.cachePath + '/**/*.*' + ], { + // Content of the cache directory is readable only for webserver. + // Thus we must to set "strict" option to false to prevent "EACCES" + // errors. At the same we need to see all errors that take place. + strict: false, + silent: false + }) .pipe(xgettext({ language: 'PHP', keywords: [ @@ -209,16 +224,27 @@ gulp.task('generate-pot', function() { gulp.task('pack-sources', ['composer-install'], function() { var sources = [ config.mibewPath + '/**/*', + // Exclude cache files but include ".keep" file. + '!' + config.cachePath + '/**/!(.keep)', // Exclude Git repositories that can be shipped with third-party libs '!' + config.phpVendorPath + '/**/.git', '!' + config.phpVendorPath + '/**/.git/**/*' ]; + var srcOptions = { + // Dot files (.htaccess, .keep, etc.) must be included in the package. + dot: true, + // Content of the cache directory is readable only for webserver. Thus + // we must to set "strict" option to false to prevent "EACCES" errors. + // At the same we need to see all errors that take place. + strict: false, + silent: false + } var version = config.package.version; return eventStream.merge( - gulp.src(sources, {dot: true}) + gulp.src(sources, srcOptions) .pipe(zip('mibew-' + version + '.zip')), - gulp.src(sources, {dot: true}) + gulp.src(sources, srcOptions) .pipe(tar('mibew-' + version + '.tar')) .pipe(gzip()) ) diff --git a/src/mibew/README.txt b/src/mibew/README.txt index 035730c3..821579e1 100644 --- a/src/mibew/README.txt +++ b/src/mibew/README.txt @@ -25,7 +25,8 @@ INSTALLATION 10. Change your name. 11. Wait for your visitors on 'Pending users' page. -On unix/linux platforms change the owner of /mibew/files/avatar folder -to the user, under which the web server is running (for instance, www). -The owner should have all rights on the folder /mibew/files/avatar -(chmod 700 /mibew/files/avatar). +On unix/linux platforms change the owner of /mibew/files/avatar and +/mibew/cache folders to the user, under which the web server is running +(for instance, www). The owner should have all rights on the folders +/mibew/files/avatar and /mibew/cache +(chmod 700 /mibew/files/avatar && chmod 700 /mibew/cache). diff --git a/src/mibew/cache/.keep b/src/mibew/cache/.keep new file mode 100644 index 00000000..e69de29b diff --git a/src/mibew/libs/classes/Mibew/Application.php b/src/mibew/libs/classes/Mibew/Application.php index 74a15cfc..9ef475df 100644 --- a/src/mibew/libs/classes/Mibew/Application.php +++ b/src/mibew/libs/classes/Mibew/Application.php @@ -82,11 +82,17 @@ class Application implements RouterAwareInterface, AuthenticationManagerAwareInt { $this->router = $router; $this->authenticationManager = $manager; + + $driver = new \Stash\Driver\FileSystem(); + $driver->setOptions(array('path' => MIBEW_FS_ROOT . '/cache')); + $this->cache = new \Stash\Pool($driver); + $this->assetUrlGenerator = new AssetUrlGenerator(); $this->controllerResolver = new ControllerResolver( $this->router, $this->authenticationManager, - $this->assetUrlGenerator + $this->assetUrlGenerator, + $this->cache ); $this->accessCheckResolver = new CheckResolver($this->authenticationManager); } diff --git a/src/mibew/libs/classes/Mibew/Cache/CacheAwareInterface.php b/src/mibew/libs/classes/Mibew/Cache/CacheAwareInterface.php new file mode 100644 index 00000000..4f0731d5 --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Cache/CacheAwareInterface.php @@ -0,0 +1,42 @@ +assetUrlGenerator; } + /** + * {@inheritdoc} + */ + public function getCache() + { + return $this->cache; + } + + /** + * {@inheritdoc} + */ + public function setCache(PoolInterface $cache) + { + $this->cache = $cache; + } + /** * Generates a URL from the given parameters. * diff --git a/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php b/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php index bf7012be..43390ba5 100644 --- a/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php +++ b/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php @@ -23,14 +23,17 @@ use Mibew\Asset\AssetUrlGeneratorAwareInterface; use Mibew\Asset\AssetUrlGeneratorInterface; use Mibew\Authentication\AuthenticationManagerAwareInterface; use Mibew\Authentication\AuthenticationManagerInterface; +use Mibew\Cache\CacheAwareInterface; use Mibew\Routing\RouterAwareInterface; use Mibew\Routing\RouterInterface; +use Stash\Interfaces\PoolInterface; use Symfony\Component\HttpFoundation\Request; class ControllerResolver implements RouterAwareInterface, AuthenticationManagerAwareInterface, - AssetUrlGeneratorAwareInterface + AssetUrlGeneratorAwareInterface, + CacheAwareInterface { /** * @var RouterInterface|null @@ -47,6 +50,11 @@ class ControllerResolver implements */ protected $assetUrlGenerator = null; + /** + * @var PoolInterface|null; + */ + protected $cache = null; + /** * Class constructor. * @@ -54,16 +62,19 @@ class ControllerResolver implements * @param AuthenticationManagerInterface $manager Authentication manager * instance. * @param AssetUrlGeneratorInterface $url_generator An instance of Asset - * URL generator + * URL generator. + * @param PoolInterface $cache An instance of Cache pool. */ public function __construct( RouterInterface $router, AuthenticationManagerInterface $manager, - AssetUrlGeneratorInterface $url_generator + AssetUrlGeneratorInterface $url_generator, + PoolInterface $cache ) { $this->router = $router; $this->authenticationManager = $manager; $this->assetUrlGenerator = $url_generator; + $this->cache = $cache; } /** @@ -114,6 +125,22 @@ class ControllerResolver implements return $this->assetUrlGenerator; } + /** + * {@inheritdoc} + */ + public function getCache() + { + return $this->cache; + } + + /** + * {@inheritdoc} + */ + public function setCache(PoolInterface $cache) + { + $this->cache = $cache; + } + /** * Resolves controller by request. * @@ -179,6 +206,10 @@ class ControllerResolver implements $object->setAssetUrlGenerator($this->getAssetUrlGenerator()); } + if ($object instanceof CacheAwareInterface) { + $object->setCache($this->getCache()); + } + return array($object, $method); } }