以前在创建夹具文档以构建网站时(用于测试或没有CMS的网站)。我总是按照示例文档中所示的方式完成它。因此,灯具页面会有很多重复的代码。像这样(这是一个很短的!):
class codeExampleFixtures extends AbstractFixture implements OrderedFixtureInterface
{
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
/*** Begin data fixture ***/
$code1 = '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>';
// The $exampleX variable can be left empty if not needed.
$example1 = '<p>In this example. The code is different for the example itself.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>';
$lang1 = 'html'; // choices are 'html', 'css' and 'js'
// Do not edit anything else in this fixture.
$codeExample1 = new codeExample();
$codeExample1->setlang($lang1);
$codeExample1->setcodeExample($code1);
$exampleFixture1 = (empty($example1)) ? $code1 : $example1;
$codeExample1->settextExample($exampleFixture);
$manager->persist($codeExample1);
$this->setReference('ce1', $codeExample1);
/*** End data fixture ***/
/*** Begin data fixture ***/
$code2 = '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>';
// The $exampleX variable can be left empty if not needed.
$example2 = Null;
$lang2 = 'css'; // choices are 'html', 'css' and 'js'
// Do not edit anything else in this fixture.
$codeExample2 = new codeExample();
$codeExample2->setlang($lang2);
$codeExample2->setcodeExample($code2);
$exampleFixture2 = (empty($example2)) ? $code2 : $example2;
$codeExample2->settextExample($exampleFixture2);
$manager->persist($codeExample2);
$this->setReference('ce2', $codeExample2);
/*** End data fixture ***/
$manager->flush(); //Upload the data to the database.
}
我决定重构它,我认为我的结果可能对其他做类似事情的人有用。我确信有办法改进它(我很高兴听到它们!)我相信会有很多人已经想过自己做类似的事情,但如果对你有用,请随意使用它:)
答案 0 :(得分:4)
你应该真正看看Jordi Boggiano(编写作曲家的人)nelmio/alice!
通过深入了解this blog article,了解设置灯具是多么简单,而不必使用Alice在夹具类中重复无数次。
答案 1 :(得分:1)
首先,我向名为createFixture()的类添加了一个方法:
public function createFixture($code, $example, $lang) {
$codeExample = new codeExample();
$codeExample->setlang($lang);
$codeExample->setcodeExample($code);
$exampleFixture = (empty($example)) ? $code : $example;
$codeExample->settextExample($exampleFixture);
return $codeExample;
}
然后剩下的就是将灯具放入其中。如您所见,重复的代码大幅减少:
/*** Begin data fixture ***/
$fixture = $this->createFixture(
"<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur.</p>",
"<p>In this example. The code is different from the example itself.</p>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur.</p>",
"html"
);
/* in order to reference this later, specify a simple and uniqe key for this fixture */
$this->setReference('ce1', $fixture);
$manager->persist($fixture);
/*** End data fixture ***/
/*** Begin data fixture ***/
$fixture = $this->createFixture(
"<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur.</p>",
"",
"css"
);
/* in order to reference this later, specify a simple and uniqe key for this fixture */
$this->setReference('ce2', $fixture);
$manager->persist($fixture);
/*** End data fixture ***/
显然你必须为每种不同类型的灯具重写一下,但原理是相同的。
我的完整灯具文件可以在这里找到:https://gist.github.com/alexward1981/6827216