CakePHP 2.1 Asset Compress Plugin not creating cached files - cakephp

I'm using the CakePHP Plugin AssetCompress (v 0.7) which works fine, except that it doesn't cache any files in the directory. This is my asset_compress.ini setup:
[General]
writeCache = true
cacheConfig = false
alwaysEnableController = true
debug = false
[js]
timestamp = true
paths[] = WEBROOT/js/
cachePath = WEBROOT/cache_js/
[speedtest.min.js]
files[] = speedtest/speedtest.js
Additional notes:
I set debug to "0" in core.php
the cache_js folder is writeable (777)
also I'm using MemCache as a caching engine (not sure if this might cause the issue)
Has anybody experienced the same issue with the Asset Compress plugin?
Update: This is what I use for the CSS/Less part, works pretty well: https://github.com/Hyra/less

If I understand well this Github's wiki page you should change cacheConfig = false to cacheConfig = true to take advantage of MemCache.

You have to generate the files using the shell script. The files are not automatically generated.
https://github.com/markstory/asset_compress/wiki/Shell

To generate and store static assets defined in the asset_compress.ini config or through the AssetCompress helper on the fly. This is to save you having to manually run the console script everytime you change you css or js files.
This is what some will define as a "nasty" hack, I call it a working solution. It simply runs the console script via the php exec() method every time the AppController beforeFilter() runs and the debug level is greater than 0. So in production where your debug level should be 0, the exec() won't be run.
Add the following to your /app/Controller/AppController.php beforeFilter() function.
if(Configure::read('debug') > 0){
exec(APP.'Console'.DS.'cake -app '.APP.' AssetCompress.asset_compress build -f');
}
This is assuming that you can run the normal AssetCompress from the console (linux) or cmd prompt (windows)

Related

Use variable language specific strings in hugo config file

My goal is to build a multilingual site using hugo. For this I would like to:
not touch the theme file
have a config file which defines the overall structure for all languages (config.toml)
have a "string" file for all languages
So for example, I would have a config.toml file like this:
[params.navigation]
brand = "out-website"
[params.navigation.links]
about = $ABOUT_NAME
services = $SERVICES_NAME
team = $TEAM_NAME
impressum = $IMPRESSUM_NAME
a english language file:
ABOUT_NAME=About
SERVICES_NAME=Services
TEAM_NAME=Team
IMPRESSUM_NAME=Impressum
and a german language file like this:
ABOUT_NAME=Über uns
SERVICES_NAME=Dienste
TEAM_NAME=Mitarbeiter
IMPRESSUM_NAME=Impressum
And then I want to compile the project for english, I do something along the line of:
hugo --theme=... --config=config.toml --config=english.toml
and german:
hugo --theme=... --config=config.toml --config=german.toml
Or in same similar way.
For this I need to use variables in the config.toml that are defined in english.toml or german.toml
My google search so far say, that I cannot use variables in toml.
So is there a different approach with which I could achieve this?
Your approach with variables is not optimal, use the tutorial below.
Multilingual sites are coming as a feature on Hugo 0.16, but I managed to build a multilingual site on current Hugo using this tutorial and some hacks.
The tutorial above requires to "have a separate domain name for each language". I managed to bypass that and to have to sites, one at root (EN), and one in the folder (/LT/).
Here are the steps I used.
Set up reliable build runner, you can use Grunt/Gulp, but I used npm scripts. I hacked npm-build-boilerplate and outsourced rendering from Hugo. Hugo is used only to generate files. This is important, because 1) we will be generating two sites; 2) hacks will require operations on folders, what is easy on npm (I'm not proficient on building custom Grunt/Gulp scripts).
Set up config_en.toml and config_de.toml in root folder as per tutorial.
Here's excerpt from my config_en.toml:
contentdir = "content/en"
publishdir = "public"
Here's excerpt from my config_lt.toml (change it to DE in your case):
contentdir = "content/lt"
publishdir = "public/lt"
Basically, I want my website.com to show EN version, and website.com/lt/ to show LT version. This deviates from tutorial and will require hacks.
Set up translations in /data folder as per tutorial.
Bonus: set up snippet on Dash:
{{ ( index $.Site.Data.translations $.Site.Params.locale ).#cursor }}
Whenever I type "trn", I get the above, what's left is to paste the key from translations file and I get the value in correct language.
Hack part. The blog posts will work fine on /lt/, but not static pages (landing pages). I use this instruction.
Basically, every static page, for example content/lt/duk.md will have slug set:
slug: "lt/duk"
But this slug will result in double URL path, for example lt/lt/duk.
I restore this using my npm scripts task using rsync and manual command to delete redundant folder:
"restorefolders": "rsync -a public/lt/lt/ public/lt/ && rm -rf public/lt/lt/",

How can I find the path of the current gradle script?

We use some Gradle base scripts on an central point. This scripts are included with "apply from:" from a large count of scripts. This base scripts need access to files relative to the script. How can I find the location of the base scripts?
Sample for one build.gradle:
apply from: "../../gradlebase/base1.gradle"
Sample for base1.gradle
println getScriptLocation()
I'm not sure if this is considered an internal interface, but DefaultScriptHandler has a getSourceFile() method, and the current instance is accessible via the buildscript property, so you can just use buildscript.sourceFile. It's a File instance pointing at the current script
I'm still not sure if I understood the question well but You can find path of current gradle script using following piece of code:
println project.buildscript.sourceFile
It gives the full path of the script that is currently running. Is that what You're looking for?
I'm pulling it off the stack.
buildscript {
def root = file('.').toString();
// We have to seek through, since groovy/gradle introduces
// a lot of abstraction that we see in the trace as extra frames.
// Fortunately, the first frame in the build dir is always going
// to be this script.
buildscript.metaClass.__script__ = file(
Thread.currentThread().stackTrace.find { ste ->
ste.fileName?.startsWith root
}.fileName
)
// later, still in buildscript
def libDir = "${buildscript.__script__.parent}/lib"
classpath files("${libDir}/custom-plugin.jar")
}
// This is important to do if you intend to use this path outside of
// buildscript{}, since everything else is pretty asynchronous, and
// they all share the buildscript object.
def __buildscripts__ = buildscript.__script__.parent;
Compact version for those who don't like clutter:
String r = file('.').toString();
buildscript.metaClass.__script__ = file(Thread.currentThread().stackTrace*.fileName?.find { it.startsWith r })
Another solution is set a property for the location of A.gradle in your global gradle settings at: {userhome}/.gradle/gradle.properties
My current workaround is to inject the path from the calling script. This is ugly hack.
The caller script must know where the base script is located. I save this path in a property before calling:
ext.scriptPath = '../../gradlebase'
apply from: "${scriptPath}/base1.gradle"
In base1.gradle I can also access the property ${scriptPath}
You could search for this scripts in the relative path like:
if(new File(rootDir,'../path/A.gradle').exists ()){
apply from: '../path/A.gradle'
}
This solution has not been tested with 'apply from', but has been tested with settings.gradle
Gradle has a Script.file(String path) function. I solved my problem by doing
def outDir = file("out")
def releaseDir = new File(outDir, "release")
And the 'out' directory is always next to the build.gradle in which this line is called.

Loading uncompressed js file in debug mode

Joomla has a feature where it loads the a minified javascript file and the uncompressed version when the site is in debug mode.
I have named both my files correctly and am include it as follows:
JHtml::_('script', JUri::root() . 'path_to_file/jquery-sortable.js');
When I put the site in debug mode, it does not load the uncompressed file.
However, If I use the following instead, it works fine:
JHtml::_('script', 'path_to_file/jquery-sortable.js');
Now I'm not sure whether this is a bug in Joomla or not, but I cannot find any information online regarding this. I would like to use JURI::root() in the path.
Does anyone have any information on this?
Indeed, if the script URL begins with http, the code that is responsible for including the uncompressed version (i.e, remove the min. segment if such exists or add -uncompressed otherwise) is ignored.
The source for this behavior:
JHtml::includeRelativeFiles() in libraries/cms/html/html.php:298
protected static function includeRelativeFiles($folder, $file, $relative, $detect_browser, $detect_debug)
{
// If http is present in filename
if (strpos($file, 'http') === 0)
{
$includes = array($file);
}
else
//process the script sourch.
}
...
}
Most of the script files, including frameworks, are included as relative paths. I guess that this behavior is meant to prevent remote resources from getting 404ed.

how to deal with configuration file in CakePHP

I know that in folder config are file called core.php which I can configure application options as a debug mode, session, cache etc.
But I want do configure file for my application. I want for example configure how many post can be displayed in main page, thubnails size etc.
I think that the best place in config folder but where and when parse thos file in application (bootstrap, AppController another mechanism ?) and what is the best extension .ini or PHP array (for performance reason too). What is the best practice to do this ?
DEFINE OWN CONSTANT FILE
Create a file lets suppose 'site_constants.php' containing some constant variables in app/Config folder. Define the following constants into it:
<?php
define('HTTP_HOST', "http://" . $_SERVER['HTTP_HOST'].'/');
if(HTTP_HOST == 'localhost' || HTTP_HOST == '127.0.0.1')
{
define('SITE_URL', HTTP_HOST.'app_folder_name/');
}
else
{
define('SITE_URL', HTTP_HOST);
}
Include it in app/Config/bootstrap.php
require_once('site_constants.php');
Now you can use it anywhere into your website. And this is also a dynamic.
DEFINE OWN CONFIGURATION FILE
Create a file lets suppose 'my_config.php' containing some constant variables in app/Config folder. Define the constant in the following way:
<?php
$config['PageConfig'] = array('PostPerPage' => 5, 'UserPerPage' => 15);
Then in your app/Controller/AppController.php write the following line in beforeFilter() method:
function beforeFilter()
{
Configure::load('my_config');
}
Now in your controller's method, where you want to access the page number to be listed in your pagination list. You can use it by following code:
$page_config = Configure :: read('PageConfig');
$user_per_page = $page_config['UserPerPage'];
//or
$post_per_page = $page_config['PostPerPage'];
This might looks long process to handle this task, but once done, it will help you in many senses.
Here are the advantages:
you can easily define some more constants (like any file path etc).
you can put all your ajax code into external JS files.
you can directly deploy it onto any server without changing in constants as well as work perfectly onto your localhost.
following standard conventions etc.
CakePHP provides the Configure class for this purpose. See the documentation.
You can use Configure::write($key,$value) in your own config file, and then read the values elsewhere in your application through Configure::read($key). It also allows you to use readers that automate the process and read in external configuration files. CakePHP provides a PHPreader and an INIreader by default and you can create readers to extend it.
Create a new file with configuring variables, like:
Configure::write('Twitter', array(
'consumer_key' => "OTh1sIsY0urC0n5um3rK3Y4T878676",
'consumer_secret' => "OTh1sIsY0ur53cReT76OTIMGjEhiWx94f3LV",
'oauth_access_token' => "12345678-OTh1sIsY0urAcc355K3YT878676Y723n4hqxSyI4",
'oauth_access_token_secret' => "OTh1sIsY0urACC355T0KEnsdjh4T878676FPtRRtjDA29ejYSn"
));
save this file in app/Config/twitter.php
Include that file in app/Config/bootsrap.php:
require_once('twitter.php');
In the Controller (this example 'app/Controller/TwitterController.php'), you can use that like:
$settings = Configure :: read('Twitter');

Codeigniter "file_exists($filename)"

Need help with the codeigniter, I think file_exists is for server path, not for url. but my image folder got same level with application and system folder. find nothing on google, please help
$url=base_url();
$filename="$url/upload/$id.jpg";
if (file_exists($filename)){
echo "<li><img src=\"".$url."upload/$id.jpg\" width=\"40\" height=\"40\" /></li>";
}else{
echo "<li><img src=\"http://www.mydomain.com/haha/image/noimg.png\" width=\"40\" height=\"40\" /></li>";
}
Codeigniter is always running on index.php, so all paths are relative from there. You can use any of the following, assuming upload/ is at the same level as index.php:
file_exists("upload/$id.jpg")
file_exists("./upload/$id.jpg")
file_exists(FCPATH."upload/$id.jpg")
FCPATH is a constant that Codeigniter sets which contains the absolute path to your index.php.
As a side note, I prefer is_file() when checking files, as file_exists() returns true on directories. In addition, you might want to see if getimagesize() returns FALSE to make sure you have an image.

Resources