当调用Phing运行Propel 1数据库迁移时,是否有一种干净的方法来设置类自动加载


Is there a clean way to set up class autoloading when calling Phing to run a Propel 1 database migration?

我在Propel 1中使用迁移,它运行得很好。对于一些数据操作,我需要访问Propel类,并访问常见迁移方法的父类,但由于我请求从Phing进行迁移,这似乎并不重要。

我使用以下代码调用迁移:

php '
    /project/backend-app/vendor/phing/phing/bin/phing.php '
    -f /project/backend-app/vendor/propel/propel1/generator/build.xml '
    -Dusing.propel-gen=true '
    -Dproject.dir=/project/backend-app/db '
    -Dpropel.database.url='mysql:dbname=job_crawler_test;host=127.0.0.1' '
    -Dpropel.buildtime.conf.file='buildtime/job_crawler_test.xml' '
    -quiet '
    migrate

这很好,只要我在每个需要的类文件的开头都有自动加载和初始化代码:

$root = realpath(__DIR__ . '/../..');
require_once $root . '/vendor/autoload.php';
require_once $root . '/lib/autoload.php';
set_include_path($root . '/lib' . PATH_SEPARATOR . get_include_path());
Propel::init($root . '/config/propel-conf.php');

好吧,这是有效的,但有点混乱——尽管这是官方建议(见上面手册页面链接的底部)。为了干净起见,我想去掉这个重复的代码块。

当然,我可以把它放在一个文件中,在每个文件中使用一行require,这会减少一些麻烦,但这不是很令人满意。我想知道是否有一个-D标志可以传递给Phing,也许就像引导PHP文件一样?

我想知道-Dphp.classpath是否会做点什么,因为这似乎是Phing的核心属性,但这似乎没有任何区别。

@mario在评论中提出了一个善意的建议,完美地解决了这个问题。我将自动加载前导移到了一个单独的脚本:

<?php
/*
 * db/scripts/propel-migration.php
 *
 * Bootstrap file for Propel migrations that need autoloading or access
 * to the Propel sub-system
 */
$root = realpath(__DIR__ . '/../..');
require_once $root . '/vendor/autoload.php';
require_once $root . '/lib/autoload.php';
set_include_path($root . '/lib' . PATH_SEPARATOR . get_include_path());
'Propel::init($root . '/config/JobCrawler-conf.php');

然后我修改了php调用(即,而不是phing.php调用),从而:

php '
    -d 'auto_prepend_file=/project/backend-app/db/scripts/propel-migration.php' '
    <rest of call here>

Propel迁移类现在可以完全自动加载并访问Propel的系统,而无需任何类前样板代码。

相关文章: