From a0958964f45a2d5ba89055c5fd7ddb332f4b7945 Mon Sep 17 00:00:00 2001 From: Ash Smith Date: Mon, 13 Jun 2016 14:49:05 +0100 Subject: [PATCH 1/2] Moved CSS location to media folder Fixes #5 --- readme.md | 2 +- .../Meanbee/ConfigPoweredCss/Model/Config.php | 65 ++++++++++++++++++- .../Meanbee/ConfigPoweredCss/Model/Css.php | 4 ++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 4ada846..1eb8146 100644 --- a/readme.md +++ b/readme.md @@ -19,7 +19,7 @@ The user stories that this extension should achieve and maintain are as follows. - The CSS file should be generated on clicking of "Publish" button in cache management. - Generates CSS File - CSS file should be in a directory which can be written to be the web. - - While there was a preference for media, we have chosen skin to maintain compatibility with CSS merging. + - The file is generated into the media folder, the drawback of this that the file cannot be merged when css merging is enabled. [More info](https://github.com/meanbee/magento-configuration-powered-css/issues/5) - CSS file will be plain CSS and will have no interaction with frontend build tools. - CSS file will be generated with a custom block and template. - Each theme can then have it’s own template file if required. diff --git a/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Config.php b/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Config.php index fbaf705..c6e72d3 100644 --- a/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Config.php +++ b/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Config.php @@ -6,7 +6,8 @@ class Meanbee_ConfigPoweredCss_Model_Config const XML_PATH_HEAD_BLOCK = 'dev/meanbee_configpoweredcss/head_block'; const XML_PATH_LOGGING = 'dev/meanbee_configpoweredcss/logging'; const LOG_FILENAME = 'meanbee_configpoweredcss.log'; - const CSS_FILENAME = 'css/meanbee_configpoweredcss_%d.css'; + const CSS_FILENAME = 'meanbee_configpoweredcss_%d.css'; + const CSS_PATH = 'css/config/%s/%s/'; /** * Is the extension enabled? @@ -77,6 +78,66 @@ public function getCssFilename($store = null) */ public function getFullCssFilePath($store = null) { - return Mage::getBaseDir('skin') . '/frontend/base/default/' . $this->getCssFilename($store); + return $this->getCssDirectoryPath() . $this->getCssFilename($store); + } + + /** + * Get directory for CSS files + * + * @param $store + * @return string + */ + public function getCssDirectoryPath($store = null) + { + + return Mage::getBaseDir('media') . $this->getCssPath($store); + } + + /** + * Retrieve the CSS path relative to the media directory + * @param null $store + * @return string + */ + public function getCssPath($store = null) + { + return sprintf(self::CSS_PATH, $this->_getDesignPackage($store), $this->_getDesignTheme($store)); + } + + /** + * Get full URL for CSS file + * @param null $store + * @return string + */ + public function getCssFileUrl($store = null) + { + return Mage::getUrl(sprintf('media/%s', $this->getCssPath($store))) . $this->getCssFilename($store); + } + + /** + * Get the current package for store + * @param int|null $store + * @return mixed + */ + protected function _getDesignPackage($store = null) + { + return Mage::getStoreConfig('design/package/name', $store); + } + + /** + * Get the current theme for the store + * @param int|null $store + * @return string + */ + protected function _getDesignTheme($store = null) + { + $theme = Mage::getStoreConfig('design/theme/template', $store); + if (!$theme) { + $theme = Mage::getStoreConfig('design/theme/default', $store); + } + if (!$theme) { + $theme = 'default'; + } + + return $theme; } } diff --git a/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Css.php b/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Css.php index 55c2121..a43d1ce 100644 --- a/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Css.php +++ b/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Css.php @@ -61,6 +61,10 @@ protected function _writeToFile($string, $storeId) return false; } + if (!is_dir($this->config->getCssDirectoryPath($storeId))) { + mkdir($this->config->getCssDirectoryPath($storeId), 0755, true); + } + $file = $this->config->getFullCssFilePath($storeId); $result = file_put_contents($file, $string, LOCK_EX); From ace7f8c07a4bcd7e96caeb0aeb592a8f09d8ee76 Mon Sep 17 00:00:00 2001 From: Ash Smith Date: Mon, 27 Jun 2016 14:12:22 +0100 Subject: [PATCH 2/2] Ensure directory is correct, use media url for css file url. - Fixes issue where the directory path of the generated css file could be missing a directory slash. - Fixes issue where if a CDN is used, the generated file won't use the CDN URL. Fixes potential issue where SID parameters are included in the URL, and breaks file path. --- .../Meanbee/ConfigPoweredCss/Model/Config.php | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Config.php b/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Config.php index c6e72d3..c598e6f 100644 --- a/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Config.php +++ b/src/app/code/community/Meanbee/ConfigPoweredCss/Model/Config.php @@ -90,7 +90,7 @@ public function getFullCssFilePath($store = null) public function getCssDirectoryPath($store = null) { - return Mage::getBaseDir('media') . $this->getCssPath($store); + return Mage::getBaseDir('media') . DS . $this->getCssPath($store); } /** @@ -104,13 +104,26 @@ public function getCssPath($store = null) } /** - * Get full URL for CSS file + * Generate the URL to access the stylesheet for the store. + * * @param null $store * @return string */ public function getCssFileUrl($store = null) { - return Mage::getUrl(sprintf('media/%s', $this->getCssPath($store))) . $this->getCssFilename($store); + $url_parts = array( + Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA), + $this->getCssPath($store), + $this->getCssFilename($store) + ); + + array_walk($url_parts, function (&$item) { + $item = trim($item, '/'); + }); + + $url = join('/', $url_parts); + + return $this->stripProtocolFromUrl($url); } /** @@ -140,4 +153,15 @@ protected function _getDesignTheme($store = null) return $theme; } + + /** + * Given a URL, remove the protocol. For example, "https://test.com/jpeg" becomes "//test.com/jpeg". + * + * @param string $url + * @return string + */ + protected function stripProtocolFromUrl($url) + { + return preg_replace('/^https?:/', '', $url); + } }