Optimize Eclipse and Zend studio refresh in Magento projects with the resources filters

If you’re using Zend Studio or eclipse and have kept the native structure of a Magento project (in its first version), refreshing a project could sometime be a long task.

This is partly due to the number of files that change and need to be refreshed: Magento needs to take their new content into account.

To avoid refreshing every file changing frequently, you can exclude them: they won’t appear in the project folder, and so, won’t be
refreshed

Continue reading “Optimize Eclipse and Zend studio refresh in Magento projects with the resources filters”

Cancel commits on Magento core files with subversion hooks

As a best practice, it is not recommended to update Magento core files. This can be done by Magento overloading mechanism

This morning, an old colleague of mine mentioned that a developer committed some updates in the app/code/core files.

This could be blocked by subversions hooks scripts

What is a subversion hook?

Introduction to subversions hooks: the hook events

Subversion implement a hook mechanism which allows to call some externals scripts on some repository events. This functionality allows you to customize your repository work-flow. The most common hook usages are:

  • Sending email notification (on each commit, when a tag is done, …)
  • Control access to repository (when locking files, …)

List of available hooks events can be found in the subversion documentation

Hook event we’ll use to control our commit must be executed before transaction is completed: the pre-commit hook

What subversion does on a subversion hook event?

On this predefined event list, Subversion will check if there is an executable script in the hooks folder of your subversion repository with the name of the event

If yes, subversion user will call this script with some arguments. Arguments list sent to this script depends on the hook event. For example, for the pre-commit hook script, arguments are:

  • Full path to repository involved
  • Current transaction number
  • With these arguments, you can do what you want within, especially calling all the administrative subversion commands:

    • svnadmin
    • svnlook

    Setting up a subversion hook which cancels the transaction in case core files are updated

    Here’s an example of the script you can use as pre-commit hook for Magento projects

    #!/bin/bash
    ##################################
    # render more readable parameters
    ##################################
    REPOS="$1"
    TXN="$2"
    ################
    # configuration 
    ################
    # path to svnlook script
    SVNLOOK=/usr/bin/svnlook
    #############
    # librairies
    #############
    # a method to send message to client and end transaction
    show_error_message() {
        echo $1 1>&2;
        exit $2;
    }
    ###############################
    # run
    ###############################
    # fetch the account user from the current transaction informations
    AUTHOR=`$SVNLOOK author -t "$TXN" "$REPOS"`
    
    # check if we do not updating file in core files
    $SVNLOOK changed -t "$TXN" "$REPOS" | grep "^U \+app/code/core" > /dev/null
    if test $? -eq 0 
    then 
        show_error_message "$AUTHOR, You cannot update Magento core files" 2
    fi
    

    This script will look for files involved in current transaction with the svnlook script and look for path app/code/core in the output answer. (this svnlook command lists all files and their state in a transaction). If we find something matching, we send a message on the STDERR (STDERR will be sent to subversion client) and exit script with a non 0 error code, which will cancel the transaction and so block the commit

    To use it, just copy this script content in a hooks/pre-commit file of your repository folder and make it executable by the system

    Conclusion about usage of hooks on Magento scripts

    No more reasons to leave developers committing app/code/core files with this script!

    But you can extend hooks to do many other things on your repository: block Magento core files commit, block update on tagged versions, notifications of branch creation, block commit of PHP files which contains parse errors, etc…

    Do you use these events on your Magento repositories?

    Have Zend Studio and Eclipse PDT faster with Magento projects: the build path configuration

    Eclipse PDT editors like Zend Studio analyse the PHP source code to enable autocompletion: they analyze source files and index content found in

    Magento embeds more than 10000 files (7000 php files) in Magento 1.7.0.0. Do we need to index all content? no

    To ensure that eclipse and Zend studio will offer you all the expected functionalities, you can make it faster, by removing all the non required editor files from editing build path.

    What is Zend studio and Eclipse PDT build path?

    Build path defines classpath for your project: folders that will be analyzed by editors to index content and where look for methods.

    All build path sources defined within will be made available for Content Assist options, including source code completion

    Basic configuration of the build path directive in Magento projects

    Folders that we can remove from build path could be the following ones:

  • var: contains variables Magento data. Too much volatil, there is no real reason to include it in build path
  • media: all media elements used on your website. It should contain all picture files from your website and so there is no reason to analyse binary sources
  • skin: even if skin folder can contain js files, we can remove it
  • app/locale: for the time being, there is no link between magento translations calls and translations files. so we can also remove it
  • app/etc: like translations files, we can remove it
  • If you do not enable javascript support on your magento project, you can also remove the js folder

    How to configure build path with Zend Studio and eclipse PDT?

    On your Magento project, click right on the project and edit the build path configuration menu through the menu configure build path

    You’ll have a new window which look like this one:

    And then edit the excluded folders in list. When finished, eclipse will rebuild the build path with your new configuration

    By updating the build path, you can reduce by 30 percent number of files analyzed by eclipse. Even if you use some ssd disks, your eclipse should be faster. so if you use some slower disks or use shared networks folders, you should not forget to use this configuration value

    Autocomplete in Magento projects with Zend Studio or Eclipse PDT

    Source code autocomplete is not very easy in Magento projects. But there is a way that allows to fully have these features.

    Why is source code autocomplete so complicated to have in Magento projects?

    As I mentionned before, there is multiples ways to overload native Magento API. The active overloading mechanism allows you to overload a class with another different class simply by setting up a configuration value. But usage of these overloading mechanims make difficult to provide a PHPDoc which allows autocomplete:

    • which tag @return do I need to used?
    • Which object is returned by the call of Mage::getModel(‘catalog/product’) instruction?

    Very difficult for Varien to provide an answer: it depends on your configuration, and so, autocomplete is often very difficult to have.

    However, there is a way to have it with Zend Studio or PDT: no more loosing time to search a function signature or a block content, you can have with Zend Studio or PDT the fully autocomplete of your code

    All will be done with the @var tag

    The @var tag for code source autocomplete

    Is the @var tag a PHPDoc tag?

    No: This tag is not a PHPDoc compliant tag, but an extension available to complete PHPDoc

    Syntax of @var tag

    This tag is defined as followed:

    /* @var $variable Class_Type */
    

    Be sure to provide a simple * instead of the two required in PHPDoc

    Where to put the @var tag?

    This tag must be placed after variable definition, on the same line or on the following ones: the editor will be able to match variables with related classes only after it has found this instruction.

    So there is no difficulty to do that, just take time to well comment your source code

    Usage example of @var tag in Magento source code

    Template usage of the @var tag

    Without any @var tag in our template, we can’t have any code autocomplete:

    If we have the @var tag in top part of the template, now we are able to see all availables methods:

    getModel and getSingleton instruction

    Loading classes and collection is often boring without autocomplete

    $collection = Mage::getModel('catalog/product')->getCollection();
    

    using the @var tag will offer you all availables methods. try this code in a Magento project:

    $collection = Mage::getModel('catalog/product')->getCollection();
    /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
    

    Conclusion on @var tag

    This tag allows you only with CTRL+Space shortcut to have every availables methods on objects referenced. I’m sure that you’ll find that this way will allow you to earn time in your projects