Search code examples
unixkohanacommand-line-interfacekohana-3.2kohana-3.3

How to upgrade from Kohana 3.2 to 3.3 (implementing PSR-0)?


What steps do I need tot take to upgrade from Kohana 3.2 to 3.3 with respect to the implementation of PSR-0 and what commands have to be executed from the command line?


Solution

  • Unix command line:

    These are the steps I took to implement PSR-0 in my Kohana application.

    I removed the following system/ dir:

    rm -rf system
    

    In your current bootstrap.php the only change is to make the classes start with an upper, so best is to keep your old bootstrap and just change the following lines on top of the file:

    // Load the core Kohana class
    require SYSPATH.'classes/Kohana/Core'.EXT;
    
    if (is_file(APPPATH.'classes/Kohana'.EXT))
    {
      // Application extends the core
      require APPPATH.'classes/Kohana'.EXT;
    }
    else
    {
      // Load empty core extension
      require SYSPATH.'classes/Kohana'.EXT;
    }
    

    Remove the bootstrap.php from the new kohana release dir. Now copy paste all files of 3.3 to your old application:

    cp -R path/to/new/kohana/* .
    

    Now move all your controllers and models to the capitalised dirs and remove the old dirs:

    mv application/classes/controller/* application/classes/Controller
    mv application/classes/model/* application/classes/Model
    rm -rf application/classes/controller application/classes/model
    

    The vendor dir has a fixed place in the root of the kohana dir. Move your vendor dir from application/vendor (if you have one there) to vendor/

    mv application/vendor .
    

    Edit the database config file (e.g. application/config/database.php), all "type" properties should be capitalised:

      return array
      (
        'default' => array
        (
          'type'       => 'MySQL',
    

    When you use the AUTH orm driver and you have overwritten the config in application/config/auth.php, uppercase the driver name:

    return array(
      'driver'       => 'ORM',
    

    Now comes the tricky part, all class names and file names of these classes should be capitalised. Go to the classes dir.

    cd application/classes
    

    And copy paste this command:

      for SRC in `find . -depth`
      do DST=`dirname "${SRC}"`/`basename "${SRC}" | sed -e 's/^./\U&/'`;
          if [ "${SRC}" != "${DST}" ]
          then
            [ ! -e "${DST}" ] && mv -T "${SRC}" "${DST}" || echo "${SRC} was not renamed"
          fi
      done
    

    (source: http://forum.kohanaframework.org/discussion/comment/73089#Comment_73089)

    This command recursively checks all dirs and capitalizes the filenames. Go to the application dir.

    cd ../
    

    Now inside the models and controllers (and helpers), capitalise all class names. So Controller_template_parent must become Controller_Template_Parent. I have some very inefficient commands for this one (so please contribute).

    find ./ -name \*.php -exec sed  -i "s/helper_\([a-zA-Z]\+\)/Helper_\u\1/gI" {} \;
    find ./ -name \*.php -exec sed  -i "s/helper_\([a-zA-Z]\+\)_\([a-zA-Z]\+\)/Helper_\u\1_\u\2/gI" {} \;
    find ./ -name \*.php -exec sed  -i "s/helper_\([a-zA-Z]\+\)_\([a-zA-Z]\+\)_\([a-zA-Z]\+\)/Helper_\u\1_\u\2_\u\3/gI" {} \;
    

    The ./ in the beginning is for all files (recursive dirs), that end in .php. I added a "I" behind the sed command to make the search case insensitive. (source command: https://askubuntu.com/questions/84007/find-and-replace-text-within-multiple-files)

    The first command will replace helper_some_thing_here with Helper_Some_thing_here. The second command will convert helper_some_thing_here to Helper_Some_Thing_here etc. So if you have class names with more than 3 underscores in it, you can build your own command.

    The same needs to be done for classes starting with Model_ and Controller_

    find ./ -name \*.php -exec sed  -i "s/model_\([a-zA-Z]\+\)/Model_\u\1/gI" {} \;
    find ./ -name \*.php -exec sed  -i "s/model_\([a-zA-Z]\+\)_\([a-zA-Z]\+\)/Model_\u\1_\u\2/gI" {} \;
    find ./ -name \*.php -exec sed  -i "s/model_\([a-zA-Z]\+\)_\([a-zA-Z]\+\)_\([a-zA-Z]\+\)/Model_\u\1_\u\2_\u\3/gI" {} \;
    
    find ./ -name \*.php -exec sed  -i "s/controller_\([a-zA-Z]\+\)/Controller_\u\1/gI" {} \;
    find ./ -name \*.php -exec sed  -i "s/controller_\([a-zA-Z]\+\)_\([a-zA-Z]\+\)/Controller_\u\1_\u\2/gI" {} \;
    find ./ -name \*.php -exec sed  -i "s/controller_\([a-zA-Z]\+\)_\([a-zA-Z]\+\)_\([a-zA-Z]\+\)/Controller_\u\1_\u\2_\u\3/gI" {} \;
    

    Now some class names that were used with only 1 capital, are now written in full capitals (Html, Url, Http, UTF8). Replace them in your whole application.

    Execute this command in the application/ dir:

    find ./ -name \*.php -exec sed -i "s/Url::/URL::/gI" {} \;
    find ./ -name \*.php -exec sed -i "s/Html::/HTML::/gI" {} \;
    find ./ -name \*.php -exec sed -i "s/Http::/HTTP::/gI" {} \;
    find ./ -name \*.php -exec sed -i "s/Utf8::/UTF8::/gI" {} \;
    

    When you use the ORM driver, all your Orm::factory('some_class') should be upper cased and capitalised to ORM::factory('Some_Class'). I use the same command to uppercase all ORM classes and to capitalise the class names in the factory.

    find ./ -name \*.php -exec sed -i  "s/orm::factory(\(\"\|'\)\([a-zA-Z_]\+\)\(\"\|'\)\(,[^,]*\)*)/ORM::factory('\u\2'\4)/gI" {} \;
    find ./ -name \*.php -exec sed -i  "s/orm::factory(\(\"\|'\)\([a-zA-Z]\+\)_\([a-zA-Z]\+\)\(\"\|'\)\(,[^,]*\)*)/ORM::factory('\u\2_\u\3'\5)/gI" {} \;
    find ./ -name \*.php -exec sed -i  "s/orm::factory(\(\"\|'\)\([a-zA-Z]\+\)_\([a-zA-Z]\+\)_\([a-zA-Z]\+\)\(\"\|'\)\(,[^,]*\)*)/ORM::factory('\u\2_\u\3_\u\4'\6)/gI" {} \;
    

    Now my modules where not compatible anymore since 3.3, not all of them are upgraded. When you want to upgrade them yourself, you probably have to check them separately go to the classes dir of every module, capitalise it's file and it's class names. You can use previous commands.

    For me this is a checklist when upgrading an application, like I said, please feel free to contribute so upgrading is made easier.