111 lines
2.7 KiB
PHP
111 lines
2.7 KiB
PHP
<?php
|
|
|
|
use SilverStripe\Dev\BuildTask;
|
|
use Faker\Factory;
|
|
use SilverStripe\Control\HTTPRequest;
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
use Symfony\Component\Finder\Finder;
|
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
|
use Symfony\Component\Filesystem\Exception\IOException;
|
|
|
|
class FTPageTypeCreatorTask extends BuildTask
|
|
{
|
|
/**
|
|
* @var \Faker\Factory
|
|
*/
|
|
protected $faker;
|
|
|
|
/**
|
|
* @var Filesystem
|
|
*/
|
|
protected $fs;
|
|
|
|
/**
|
|
* @var Finder
|
|
*/
|
|
protected $finder;
|
|
|
|
/**
|
|
* FTPageTypeCreatorTask constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->fs = new Filesystem();
|
|
$this->faker = Factory::create();
|
|
$this->finder = new Finder();
|
|
}
|
|
|
|
/**
|
|
* @param HTTPRequest $request
|
|
*/
|
|
public function run($request)
|
|
{
|
|
$count = $request->getVar('count') ?: 20;
|
|
$module = ModuleLoader::getModule('silverstripe/frameworktest');
|
|
$testPageDir = $module->getPath() . '/code/test-pages';
|
|
if (!$this->fs->exists($testPageDir)) {
|
|
throw new RuntimeException("Test page directory $testPageDir does not exist!");
|
|
}
|
|
|
|
$pageTypes = $this->getExistingClassNames($testPageDir);
|
|
$created = 0;
|
|
while ($created < $count) {
|
|
$className = null;
|
|
while (
|
|
!$className ||
|
|
in_array($className, $pageTypes ?? []) ||
|
|
class_exists(basename($className ?? '', 'php'))
|
|
) {
|
|
$className = $this->generateClassName();
|
|
}
|
|
$pageTypes[] = $className;
|
|
$code = $this->generateClassCode($className);
|
|
$filePath = sprintf('%s/%s.php', $testPageDir, $className);
|
|
try {
|
|
$this->fs->dumpFile($filePath, $code);
|
|
$created++;
|
|
} catch (IOException $e) {
|
|
echo "Could not write to file $filePath. Got error: {$e->getMessage()}\n";
|
|
die();
|
|
}
|
|
echo "Created page type $className\n";
|
|
}
|
|
}
|
|
|
|
private function getExistingClassNames($dir)
|
|
{
|
|
$files = $this->finder
|
|
->in($dir)
|
|
->files()
|
|
->name('*Page.php');
|
|
|
|
return array_map(function ($file) {
|
|
/* @var SplFileInfo $file */
|
|
return $file->getBasename();
|
|
}, iterator_to_array($files));
|
|
}
|
|
|
|
private function generateClassName()
|
|
{
|
|
return ucfirst($this->faker->word . 'Page');
|
|
}
|
|
|
|
private function generateClassCode($className)
|
|
{
|
|
$self = __CLASS__;
|
|
$code = <<<PHP
|
|
<?php
|
|
|
|
/**
|
|
* Generated by $self
|
|
*/
|
|
class $className extends Page implements TestPageInterface
|
|
{
|
|
|
|
}
|
|
PHP;
|
|
return $code;
|
|
}
|
|
|
|
} |