Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

Moved CSS location to media folder #6

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
65 changes: 63 additions & 2 deletions src/app/code/community/Meanbee/ConfigPoweredCss/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a trailing slash after media directory Mage::getBaseDir('media') . DS . $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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should strip the protocol from the URL. Currently this won't work if SSL is terminated at a load balancer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should fetch the media url properly: Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

If you're using a CDN, currently the URL will only be the base url of the store.

Also, it's possible SSID parameters to be included in this URL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented this like:

/**
 * Generate the URL to access the stylesheet for the store.
 *
 * @param null $store
 * @return string
 */
public function getCssFileUrl($store = null)
{
    $url_parts = [
        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);
}

}

/**
* 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;
}
}
4 changes: 4 additions & 0 deletions src/app/code/community/Meanbee/ConfigPoweredCss/Model/Css.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down