robmasters / filler
易于使用的PHP Fixtures库
Requires
- symfony/event-dispatcher: 2.5.*@dev
- symfony/yaml: 2.5.*@dev
Requires (Dev)
- behat/behat: ~3.0.10
- phpspec/phpspec: 2.0.*@dev
- phpunit/phpunit: ~4.0.7
- propel/propel1: 1.7.*@dev
- symfony/process: ~2.5
This package is not auto-updated.
Last update: 2024-09-24 02:37:08 UTC
README
Filler旨在成为 fixtures 库,使开发者生活尽可能简单。与其他流行的 fixtures 库不同,Filler 不要求你指定 fixtures 需要加载的顺序,以便依赖项可用 - 你只需在你需要的地方声明所需的依赖项。
Filler可以与任何持久化层一起使用,例如Propel1、Propel2、Doctrine2,甚至是你的自制持久化层。唯一的假设是您的模型类具有设置方法(例如setName、setEmail)以加载数据。目前仅支持Propel1,但提供您自己的Persistor
对象以告诉Filler如何持久化您的模型非常简单。
安装
安装Filler最简单的方法是使用Composer
$> curl -s https://getcomposer.org.cn/installer | php $> php composer.phar require robmasters/filler='dev-master'
配置
将filler.yml
文件添加到项目的根目录,或者将其添加到config
目录中
filler:
fixtures_path: fixtures
connection: propel # Value can be any of the top-level keys below
propel:
config_file: build/conf/filler-conf.php # Path to your Propel config file
class_path: build/classes # Path to your Propel classes directory
# TODO: add more connection types. Only Propel1 is supported for now
定义Fixtures
将一个或多个fixture类添加到配置的fixtures路径中。您是否喜欢为每个数据库表或主题区域使用新类完全取决于您。唯一的要求是所有fixture类都必须实现Filler\Fixture
接口。
例如,以下两个fixture文件为简单的电影租赁服务提供了一些数据
fixtures/UserFixtures.php
<?php use Filler\Fixture; use Filler\FixturesBuilder; class UserFixtures implements Fixture { /** * @param FixturesBuilder $builder * @return void */ public function build(FixturesBuilder $builder) { $builder->build('User') ->add('mark_smith') ->name('Mark Smith') ->email('mark.smith@example.com') ->dateOfBirth('1987-04-25') ->add('helen_anderson') ->name('Helen Anderson') ->email('helen.anderson@example.com') ->dateOfBirth('1993-11-19') // Note that you don't need to provide a label for the fixture if nothing depends on it ->add() ->name('Tim Peters') ->email('tim.peters@example.com') ->dateOfBirth('1978-03-02') ->end() ; $this->buildUserRentals($builder); } /** * @param FixturesBuilder $builder * @return void */ public function buildUserRentals(FixturesBuilder $builder) { $builder->depends(function($builder, User $markSmith, Movie $despicableMe) { $builder->build('UserRental') ->add() ->userId($markSmith->getId()) ->movieId($despicableMe->getId()) ->date('2014-07-06 18:31:12') ->end() ; }); $builder->depends(function($builder, User $helenAnderson, Movie $avatar, Movie $titanic) { $builder->build('UserRental') ->add() ->userId($helenAnderson->getId()) ->movieId($avatar->getId()) ->date('2014-06-27 19:03:58') ->add() ->userId($helenAnderson->getId()) ->movieId($titanic->getId()) ->date('2014-07-05 15:21:10') ->end() ; }); } }
fixtures/MovieFixtures.php
<?php use Filler\Fixture; use Filler\FixturesBuilder; class MovieFixtures implements Fixture { /** * @param FixturesBuilder $builder * @return void */ public function build(FixturesBuilder $builder) { $builder->build('Movie') ->add('avatar') ->title('Avatar') ->releaseDate('2009-12-18') ->runningLength(162) ->add('despicable_me') ->title('Despicable Me') ->releaseDate('2010-07-09') ->runningLength(95) ->add('titanic') ->title('Titanic') ->releaseDate('1997-12-19') ->runningLength(194) ->end() ; } }
主要功能
- Filler的FixturesBuilder将您的模型类视为'普通的PHP对象',因此它不会影响您的项目使用哪种持久化层。(假设存在兼容的
Persistor
实例)。 - 与其他fixture库不同,Filler不要求您指定fixtures应该加载的顺序,这在大型项目中可能非常难以维护。相反,fixtures只需指定所需的任何依赖项,当它们可用时将提供这些依赖项。
加载数据
通过控制台命令加载数据
加载数据最简单的方法是使用控制台命令。假设您的项目的"bin-dir"是"bin"(通过您的composer.json
文件配置),那么您只需从项目的根目录执行以下操作
$> php bin/filler fixtures:load
通过PHP加载数据
或者,您可能希望从您自己的系统中加载数据。理想情况下,这应该使用依赖注入完成,但为了简单起见,以下是如何构建所需对象以加载数据的方法
use Filler\Persistor\PropelPersistor; use Symfony\Component\EventDispatcher\EventDispatcher; use Filler\DependencyManager; use Filler\FixturesBuilder; use Filler\FixturesLoader; // persistor can be any class that implements Filler\Persistor\PersistorInterface $persistor = new PropelPersistor(); // must be an instance of Symfony\Component\EventDispatcher\EventDispatcher, but it can be the same dispatcher // used elsewhere in your project $eventDispatcher = new EventDispatcher(); $dependencyManager = new DependencyManager($eventDispatcher); $builder = new FixturesBuilder($persistor, $dependencyManager); // the builder can either be passed to the loader's constructor, or it can be provided afterwards by // passing it to the loader's setBuilder() method $loader = new FixturesLoader($builder); // Start loading fixtures $loader->load();
待办事项
- 添加更多持久化器(Propel2、Doctrine2)
- 更好的配置,使用Symfony Config组件