<?php
/**
 * Based entirely off of dereuromark's plugins as I was having trouble getting fixtures to load
 * after moving the plugins outside of an existing cakephp app's plugins folder
 *
 * @link https://github.com/dereuromark/cakephp-tools/blob/master/tests/schema.php
 */
use Cake\Utility\Inflector;

$tables = [];

/**
 * @var \DirectoryIterator<\DirectoryIterator> $iterator
 */
$iterator = new DirectoryIterator(__DIR__ . DS . 'Fixture');
foreach ($iterator as $file) {
	if (!preg_match('/(\w+)Fixture.php$/', (string)$file, $matches)) {
		continue;
	}

	$name = $matches[1];
	$tableName = Inflector::underscore($name);
	$class = 'CakeProducts\\Test\\Fixture\\' . $name . 'Fixture';
	try {
		$object = (new ReflectionClass($class))->getProperty('fields');
	} catch (ReflectionException $e) {
		continue;
	}

	$array = $object->getDefaultValue();
	$constraints = $array['_constraints'] ?? [];
	$indexes = $array['_indexes'] ?? [];
	unset($array['_constraints'], $array['_indexes'], $array['_options']);
	$table = [
		'table' => $tableName,
		'columns' => $array,
		'constraints' => $constraints,
		'indexes' => $indexes,
	];
	$tables[$tableName] = $table;
}

return $tables;