如何从Kohana 3.2升级到3.3(实现PSR-0)?

问题描述:

关于PSR-0的实现,我需要采取什么步骤从Kohana 3.2升级到3.3,以及必须从命令行执行哪些命令?

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?

Unix命令行:

这些是我在Kohana应用程序中实现PSR-0的步骤.

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

我删除了以下系统/目录:

I removed the following system/ dir:

rm -rf system

在当前的bootstrap.php中,唯一的更改是使类从上层开始,因此最好是保留旧的bootstrap并仅在文件顶部更改以下几行:

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;
}

从新的kohana发行目录中删除bootstrap.php.现在,将3.3的所有文件复制粘贴到您的旧应用程序中:

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

供应商目录在kohana目录的根目录中有固定的位置.将您的供应商目录从应用程序/供应商(如果您在那里有)移动到供应商/

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 .

编辑数据库配置文件(例如application/config/database.php),所有类型"属性均应大写:

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

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

当您使用AUTH orm驱动程序并且覆盖了application/config/auth.php中的配置时,请使用大写的驱动程序名称:

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

然后复制粘贴此命令:

  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

(来源: 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 ../

现在在模型和控制器(以及帮助器)中,将所有类名大写.因此,Controller_template_parent必须成为Controller_Template_Parent.对于此命令,我有一些效率很低的命令(因此请贡献力量).

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" {} \;

./开头的所有文件(递归目录)以.php结尾.我在sed命令后面添加了"I",以使搜索不区分大小写. (源命令: https://askubuntu.com/questions/84007/find-并在多个文件中替换文本)

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)

第一个命令将用helper_Some_thing_here替换helper_some_thing_here.第二个命令会将helper_some_thing_here转换为Helper_Some_Thing_here等.因此,如果您的类名中包含超过3个下划线,则可以构建自己的命令.

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.

对于以Model_和Controller_开头的类,需要执行相同的操作

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" {} \;

现在,某些只用1个大写字母使用的类名现在以全大写字母(Html,Url,Http,UTF8)编写.在您的整个应用程序中替换它们.

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" {} \;

使用ORM驱动程序时,所有Orm :: factory('some_class')均应使用大写字母并大写为ORM :: factory('Some_Class').我使用相同的命令将所有ORM类大写,并在工厂中使用大写的类名.

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" {} \;

现在我的模块从3.3开始不再兼容,不是所有模块都已升级.当您想自己升级它们时,您可能必须单独检查它们,然后转到每个模块的类目录,将其文件和大写的名称大写.您可以使用以前的命令.

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.