/
sfPropelDataLoadTask.class.php
106 lines (84 loc) · 3.28 KB
/
sfPropelDataLoadTask.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once(dirname(__FILE__).'/sfPropelBaseTask.class.php');
/**
* Loads YAML fixture data.
*
* @package symfony
* @subpackage propel
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfPropelDataLoadTask.class.php 23922 2009-11-14 14:58:38Z fabien $
*/
class sfPropelDataLoadTask extends sfPropelBaseTask
{
/**
* @see sfTask
*/
protected function configure()
{
$this->addArguments(array(
new sfCommandArgument('dir_or_file', sfCommandArgument::OPTIONAL | sfCommandArgument::IS_ARRAY, 'Directory or file to load'),
));
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'cli'),
new sfCommandOption('append', null, sfCommandOption::PARAMETER_NONE, 'Don\'t delete current data in the database'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'),
));
$this->namespace = 'propel';
$this->name = 'data-load';
$this->briefDescription = 'Loads YAML fixture data';
$this->detailedDescription = <<<EOF
The [propel:data-load|INFO] task loads data fixtures into the database:
[./symfony propel:data-load|INFO]
The task loads data from all the files found in [data/fixtures/|COMMENT].
If you want to load data from specific files or directories, you can append
them as arguments:
[./symfony propel:data-load data/fixtures/dev data/fixtures/users.yml|INFO]
The task use the [propel|COMMENT] connection as defined in [config/databases.yml|COMMENT].
You can use another connection by using the [--connection|COMMENT] option:
[./symfony propel:data-load --connection="name"|INFO]
If you don't want the task to remove existing data in the database,
use the [--append|COMMENT] option:
[./symfony propel:data-load --append|INFO]
If you want to use a specific database configuration from an application, you can use
the [application|COMMENT] option:
[./symfony propel:data-load --application=frontend|INFO]
EOF;
}
/**
* @see sfTask
*/
protected function execute($arguments = array(), $options = array())
{
$databaseManager = new sfDatabaseManager($this->configuration);
if (count($arguments['dir_or_file']))
{
$fixturesDirs = $arguments['dir_or_file'];
}
else
{
$fixturesDirs = array_merge(array(sfConfig::get('sf_data_dir').'/fixtures'), $this->configuration->getPluginSubPaths('/data/fixtures'));
}
$data = new sfPropelData();
$data->initialize($this->dispatcher, $this->formatter);
$data->setDeleteCurrentData(!$options['append']);
$dirs = array();
foreach ($fixturesDirs as $fixturesDir)
{
if (!is_readable($fixturesDir))
{
continue;
}
$this->logSection('propel', sprintf('load data from "%s"', $fixturesDir));
$dirs[] = $fixturesDir;
}
$data->loadData($dirs, $options['connection']);
}
}