Skip to content

Instantly share code, notes, and snippets.

@fgm
Created January 4, 2014 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgm/8261606 to your computer and use it in GitHub Desktop.
Save fgm/8261606 to your computer and use it in GitHub Desktop.
A Symfony2 Console command to list the Doctrine DBAL valid column types and options.
<?php
/**
* @author: Frederic G. MARAND <fgm@osinet.fr>
*
* @copyright (c) 2013 Ouest Systèmes Informatiques (OSInet).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace OSInet\PlusVite;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\Type;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
class SchemaReferenceCommand extends Command {
/**
* Return an array of the strings to use as type names for DBAL Columns.
*
* @return array
*/
protected function getTypes() {
$typeRc = new \ReflectionClass('\Doctrine\DBAL\Types\Type');
$types = array_values($typeRc->getConstants());
sort($types);
return $types;
}
/**
* Return the list of portable options for DBAL Columns.
*
* Assumption: valid portable options are those which combine:
* - being returned as a key by Column::toArray()
* - having both a get(Option) and a set(Option) method
* - not being "columnDefinition" or "type".
*
* Although some other options would be accepted and applied by Column::set*()
* methods, the result would probably not be safe.
*
* @return string[]
*/
protected function getOptions() {
// First, the basic description of a portable column instance.
$column = new Column('foo', Type::getType('smallint'));
$arrayOptions = $column->toArray();
// Then fetch all methods on Column
$optionsRc = new \ReflectionClass('\Doctrine\DBAL\Schema\Column');
$candidateMethodOptions = array_map(function (\ReflectionMethod $method) {
return $method->getName();
}, $optionsRc->getMethods());
// Restrict them to those for which both a get* and set* exist.
$regexp = '/^([gs]et)([A-Z][\w]+)$/';
$methodOptions = array();
foreach ($candidateMethodOptions as $candidateOption) {
if ($base = preg_match($regexp, $candidateOption, $matches)) {
$name = lcfirst($matches[2]);
if (!isset($methodOptions[$name])) {
$methodOptions[$name] = 0;
}
$methodOptions[$name]++;
}
}
$methodOptions = array_filter($methodOptions, function ($count) {
return $count === 2;
});
// Finally, remove some values we probably do not want.
$invalidOptions = array(
'columnDefinition' => 1,
'type' => 1,
);
$options = array_keys(array_diff_key(
array_intersect_key($arrayOptions, $methodOptions),
$invalidOptions));
sort($options);
return $options;
}
/**
* {@inheritdoc}
*/
protected function configure() {
$this->setName('dbal:reference')
->setDescription('Provide a reference to the data types and options for Doctrine DBAL columns.')
->setHelp('The command output is valid Yaml, meaning you can inject it elsewhere easily.');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$output->writeln(Yaml::dump(array(
'types' => $this->getTypes(),
'portable_options' => $this->getOptions(),
)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment