home | contact us
» Posts tagged "path"

Items Tagged: path


If you want the functionality of realpath, to take a path containing relative elements such as /../ then this little function is exactly what you are looking for.

PHP’s built in realpath function will return false if the file or folder does not exist. Unfortunately when using complex paths then this can make debugging things a little complicated.

To resolve this simply use this function:

function normalizePath($path) {
    return array_reduce(explode('/', $path), create_function('$a, $b', '
			if($a === 0)
				$a = "/";

			if($b === "" || $b === ".")
				return $a;

			if($b === "..")
				return dirname($a);

			return preg_replace("/\/+/", "/", "$a/$b");
		'), 0);
}

For example:

$path = dirname(__FILE__) . '/../../../folder/';
$path = normalizePath($path);
echo $path; // /var/www/vhosts/domain.com/folder

 

Having issues with javascript dependencies or awkwardly structured html defining objects you need before run?

This is a little trick I used combined with csnover’s roundrect.js to provide IE versions < 9 with border-radius rounded corners.

Create a separate source file e.g. mylateloader.js and in it include the following code :-

function myInArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        var str = jQuery(haystack[i]).attr(‘src’);
        if(typeof(str) != ‘undefined’ && str.search(‘.*’+needle+’.*’) > -1) return str;
    }
    return false;
}

jQuery(document).ready(function(){
    var script = document.createElement(‘script’);
    url = myInArray(‘mylateloader.js’, document.getElementsByTagName(‘script’));
    script.src = url.replace(‘mylateloader.js’,'mycoolminifiedfile.min.js’);
    document.getElementsByTagName(‘head’)[0].appendChild(script);
    setTimeout("mycoolobject.run()",2000);
})

That way, at document.ready(), mycoolminifiedfile.min.js is loaded from the same directory on the server as the late loader js and 2 seconds later, the mycoolobject.run() is called.


 

If you use Netbeans along with Xdebug to facilitate step through debugging when coding PHP then you may come across this issue.

It’s possible to get it into a semi working scenario where you can have working breakpoints and see variable values but you have no idea which bit of code you are actually stepped up to.

This can happen if your server path and project path are not correctly configured.

To resolve this simply go to your project properties, run configuration and then hit the advanced button.

The server path should be the absolute path to the project root on the server (eg /home/server/sites/blah/) and the project path should be the path on your local machine to the project files (eg /home/joseph/projects/blah/).

Stop any debugging sessions and then start another one up and if you have the paths set right, you should get the behaviour you expect where Netbeans highlights the current line of code and allows you to step through the code to figure out what’s going on.


 

Just stumbled across a simple and incredibly useful Netbeans plugin.

http://plugins.netbeans.org/plugin/676/path-tools

Path Tools.

It’s not listed in the standard list of available plugins, but if you download the nbm file and just drag and drop it into the plugin install dialog window it works easily enough.

Once installed you can right click any folder or file and explore in the OS file manager (Nautilus for example). You can also set up shell commands. From this point your imagination is your only restriction, this gives you easy integration to various external tools.

Nice :)


 

A while ago we posted a technique for displaying template path hints in admin which involved running a couple of DB queries. That worked, but a nicer method is to create a custom module with some special XML config to get it configurable as standard.

<?xml version="1.0"?>
<config>
    <sections>
        <dev translate="label" module="core">
            <groups>
                <debug translate="label">
                    <fields>
                        <template_hints translate="label">
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>20</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </template_hints>
                        <template_hints_blocks translate="label">
                            <label>Add Block Names to Hints</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>21</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </template_hints_blocks>
                    </fields>
                </debug>
            </groups>
        </dev>
    </sections>
</config>

Original credits and full instructions here:
http://www.deanoj.co.uk/web-development/magento/magento-template-path-hints-in-adminhtml/

Cheers Deano :)


 

If you want to calculate the actual save path that Magento will use for an image file for example then you might find yourself scouring the source code for the specific method that does this.

It’s not the easiest to find, in fact its tucked away in the lib folder inside the Varien_File_Uploader class.

The method in question is a usefully static method Varien_File_Uploader::getDispretionPath();

Sorted!


 

Bash scripts are often used to work on files and the current working directory is of paramount importance.

However, you may have scripts that are running in a development environment and also a live environment with different folder setups.

To help with portability and remove the requirement to hard code paths, you can use this little snippet

DIR="$( cd "$( dirname "$0" )" && pwd )"

For example, I have a system all running from a folder with one subfolder called processes (where my scripts are located) and another folder called launcher which actually handles the application.

The processes scripts generally launch a sequence of actions in one go.

Here is how I am using the above snippet


#!/bin/bash

DIR="$( cd "$( dirname "$0" )" && pwd )"

cd $DIR

cd ../launcher

pwd

./shellLauncher.php Scraper importCsvFile

./shellLauncher.php Scraper scrapeNewProducts

This script cd’s to the script location first (the processes sub folder) then goes back up the folder structure and into the launcher folder before launching the individual actions that make up this process.


 

If you use Calibre to manage your ebooks and ereading device, then this might be of interest.

Calibre is an excellent open source ebook management system, however some elements of its configuration are a little complex.

If you want to save books in a custom folder structure then its easy (and very powerful) with liberal use of regex or regex like patterns.

For reference, here is my send to devices save template:

If you want to learn more about it see this manual page:

http://calibre-ebook.com/user_manual/template_lang.html#templatelangcalibre


 

If you love template path hints in Magento for quickly figuring out which template file or block you need to edit or override and have a requirement for some admin side coding, you are going to love this.

You might not have thought it was possible to enable template path hints in admin, but it is!

Just run this query:

INSERT INTO core_config_data (scope, scope_id, path, value)
VALUES ('default', 0, 'dev/debug/template_hints', 1),
('default', 0, 'dev/debug/template_hints_blocks', 1);

To disable them again, run this query

UPDATE core_config_data set value = 0 where scope = 'default' and scope_id = 0 and path ='dev/debug/template_hints'

To enable again run this query

UPDATE core_config_data set value = 1 where scope = 'default' and scope_id = 0 and path ='dev/debug/template_hints'

 

If you work with applications that have a lot of nested directories (ahem Magento) then you might really like this.

In one command, you can make an entire path of nested directories with the command mkdirhier.

eg

mkdirhier EC/Custom/Model/Blah/Something/Really/Deep

You need to have xutils-dev installed

sudo apt-get install xutils-dev

Oh yeah of course you have to be running a proper developers operating system as well :)


 
rss icon