Suggestion: Save the css
- almooj-craig
- Offline Topic Author
- New Member
- Posts: 13
- Thanks: 2
Hello Ced,
I think as a general rule .css files which are basically static should be saved as a flat .css file and also have a .gz file.
When you create the .css file that's specific to the value of menuID you use php to place the menuID into the .css file which is necessary, but I think that php file should only be run once and then the output saved to a flat file.
Here is how I changed your code. (just pasted the relevant part)
Then I added this function to write and check the file.
Thanks for the very nice Maximenu_CK menu,
Craig
PS The other advantage of having a flat .css file is that it's easier to parse out the url in programs like JCH_Optimize. For example even if you get the php file to get processed by JCH_Optimize by changing the regexp, it fails because there is an eval problem, if you use a flat file it works correctly.
I think as a general rule .css files which are basically static should be saved as a flat .css file and also have a .gz file.
When you create the .css file that's specific to the value of menuID you use php to place the menuID into the .css file which is necessary, but I think that php file should only be run once and then the output saved to a flat file.
Here is how I changed your code. (just pasted the relevant part)
Code:
} else {
//$document->addStyleSheet(JURI::base() . 'modules/mod_maximenu_CK/themes/' . $theme . '/css/moo_maximenuH_CK.css');
$document->addStyleSheet('/modules/mod_maximenu_CK/themes/' . $theme . '/css/moo_maximenuH_CK.css');
if ($usecss == 1)
{
$path = '/modules/mod_maximenu_CK/themes/' . $theme . '/css/maximenuH_CK-' . $menuID.'.css';
if (file_exists(JPATH_ROOT. $path))
$document->addStyleSheet($path);
else
{
$file = '/modules/mod_maximenu_CK/themes/' . $theme . '/css/maximenuH_CK.php?monid=' . $menuID;
if (! modmaximenu_CKvmHelper::createCSS($file, JPATH_ROOT.$path))
$document->addStyleSheet('/modules/mod_maximenu_CK/themes/' . $theme . '/css/maximenuH_CK.php?monid=' . $menuID);
else
$document->addStyleSheet($path);
}
}
}
Then I added this function to write and check the file.
Code:
function createCSS($file, $path)
{
$data = file_get_contents(JURI::base().$file);
$fp = @fopen($path, "wb");
if ($fp) {
if (true or $this->_locking) {
@flock($fp, LOCK_EX);
}
$len = strlen($data);
@fwrite($fp, $data, $len);
if (true or $this->_locking) {
@flock($fp, LOCK_UN);
}
@fclose($fp);
$written = true;
}
// Data integrity check
if ($written && ($data == file_get_contents($path))) {
$fp = @fopen($path.'.gz', "wb");
if ($fp) {
if (true or $this->_locking) {
@flock($fp, LOCK_EX);
}
$data = gzcompress($data, 9);
$len = strlen($data);
@fwrite($fp, $data, $len);
if (true or $this->_locking) {
@flock($fp, LOCK_UN);
}
@fclose($fp);
}
return true;
}
else
return false;
}
Thanks for the very nice Maximenu_CK menu,
Craig
PS The other advantage of having a flat .css file is that it's easier to parse out the url in programs like JCH_Optimize. For example even if you get the php file to get processed by JCH_Optimize by changing the regexp, it fails because there is an eval problem, if you use a flat file it works correctly.
Last Edit:11 years 6 months ago
by almooj-craig
Last edit: 11 years 6 months ago by almooj-craig.
The following user(s) said Thank You: ced1870
Please Log in or Create an account to join the conversation.
Hi
thank you for your great suggestion. This is very interesting. My questions now
1/ why you don't have used the JFile::read and JFile::write function to read and write the file ?
2/ your method is very interesting, but what happen if the user change the module ID several times ? does it write multiple files without deleting the old ones ?
However I will think about your idea, because I think that there is something to do.
CEd
thank you for your great suggestion. This is very interesting. My questions now
1/ why you don't have used the JFile::read and JFile::write function to read and write the file ?
2/ your method is very interesting, but what happen if the user change the module ID several times ? does it write multiple files without deleting the old ones ?
However I will think about your idea, because I think that there is something to do.
CEd
Joomlack Webmaster and Developer
by ced1870
Please Log in or Create an account to join the conversation.
- almooj-craig
- Offline Topic Author
- New Member
- Posts: 13
- Thanks: 2
Ced,
(1) I use that method or reading and writing because it's very similar to the way the file handling cache works. See:
/libraries/joomla/cache/storage/file.php
I have never really used the JFile::read or JFile::write functions. Do you think they would work better?
(2) In reality how many times do users change the menuID name, most of the time I think they just use the default name. If they did change the menuID name or the theme it would leave an unnecessary file. The same hold true if they use an image for something then upload an new renamed image and forget to delete the old image.
Overall the occasional straggler file is a small drawback but I think it far outweighed by the thousands of times the server would need to process the PHP css file. As you know the page can not "Start to Render" until it has read all of the css files and loaded at least one image from the css file, using a flat css file that's .gz is the quickest because the server does not even need to deflate it, it just sends out the gzip "9" file.
Craig
(1) I use that method or reading and writing because it's very similar to the way the file handling cache works. See:
/libraries/joomla/cache/storage/file.php
I have never really used the JFile::read or JFile::write functions. Do you think they would work better?
(2) In reality how many times do users change the menuID name, most of the time I think they just use the default name. If they did change the menuID name or the theme it would leave an unnecessary file. The same hold true if they use an image for something then upload an new renamed image and forget to delete the old image.
Overall the occasional straggler file is a small drawback but I think it far outweighed by the thousands of times the server would need to process the PHP css file. As you know the page can not "Start to Render" until it has read all of the css files and loaded at least one image from the css file, using a flat css file that's .gz is the quickest because the server does not even need to deflate it, it just sends out the gzip "9" file.
Craig
Last Edit:11 years 6 months ago
by almooj-craig
Last edit: 11 years 6 months ago by almooj-craig.
Please Log in or Create an account to join the conversation.
1/ JFile comes from the Joomla API, so I think it is always better to use it as it manages alone all what is needed.And in fact your code would be much shorter and easier
2/ yes I agree, just not for the fact when you say that it is not important if there are a lot of useless files (I know the users ... ).But for the working side it is ok.
However I notive all this and I will work on a such solution
Ced
2/ yes I agree, just not for the fact when you say that it is not important if there are a lot of useless files (I know the users ... ).But for the working side it is ok.
However I notive all this and I will work on a such solution
Ced
Joomlack Webmaster and Developer
by ced1870
Please Log in or Create an account to join the conversation.
- almooj-craig
- Offline Topic Author
- New Member
- Posts: 13
- Thanks: 2
Ced,
(1) I implemented your suggestion about using JFile for writing but after reading through the JFile::Read code, I don't think it will interpret PHP like file_get_contents.
Here is the new function I am using:
(2) I didn't want to imply that useless files are not important, just that I did not think there would be too many since most people probably just end up using your default name.
If it's a concern, whenever a new static css file is written, you could also remove all of the useless files. I way I would do that is to first grab all of your published menuID names from the jos_modules file. Then traverse through your theme directories and delete all of the static css files that don't match a menuID name that is published.
Craig
(1) I implemented your suggestion about using JFile for writing but after reading through the JFile::Read code, I don't think it will interpret PHP like file_get_contents.
Here is the new function I am using:
Code:
function createCSS($file, $path)
{
$data = file_get_contents(JURI::base().$file);
$ret = JFile::Write($path, $data);
if ($ret && ($data == JFile::Read($path)))
{
$data = gzcompress($data, 9);
return JFile::Write($path.'.gz', $data);
}
else
return false;
}
(2) I didn't want to imply that useless files are not important, just that I did not think there would be too many since most people probably just end up using your default name.
If it's a concern, whenever a new static css file is written, you could also remove all of the useless files. I way I would do that is to first grab all of your published menuID names from the jos_modules file. Then traverse through your theme directories and delete all of the static css files that don't match a menuID name that is published.
Craig
by almooj-craig
Please Log in or Create an account to join the conversation.
Time to create page: 0.702 seconds