Moved some classes to the Symfony2 core (Propel Bridge)

This commit is contained in:
William DURAND 2011-11-03 22:22:58 +01:00
parent 1ee6477f30
commit 0bfe4840de
10 changed files with 5 additions and 962 deletions

View file

@ -10,10 +10,9 @@
namespace Propel\PropelBundle\Controller;
use Symfony\Bridge\Propel1\DataCollector\PropelDataCollector;
use Symfony\Component\DependencyInjection\ContainerAware;
use Propel\PropelBundle\DataCollector\PropelDataCollector;
/**
* PanelController is designed to display information in the Propel Panel.
*

View file

@ -1,127 +0,0 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* The PropelDataCollector collector class collects information.
*
* @author William DURAND <william.durand1@gmail.com>
*/
class PropelDataCollector extends DataCollector
{
/**
* Propel logger
*
* @var Propel\PropelBundle\Logger\PropelLogger
*/
private $logger;
/**
* Propel configuration
*
* @var \PropelConfiguration
*/
protected $propelConfiguration;
/**
* Constructor
*
* @param \Propel\PropelBundle\Logger\PropelLogger $logger A PropelLogger
*/
public function __construct(\Propel\PropelBundle\Logger\PropelLogger $logger, \PropelConfiguration $propelConfiguration)
{
$this->logger = $logger;
$this->propelConfiguration = $propelConfiguration;
}
/**
* {@inheritdoc}
*
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data = array(
'queries' => $this->buildQueries(),
'querycount' => $this->countQueries(),
);
}
/**
* Returns the collector name.
*
* @return string The collector name.
*/
public function getName()
{
return 'propel';
}
/**
* Creates an array of Build objects.
*
* @return array An array of Build objects
*/
private function buildQueries()
{
$queries = array();
$outerGlue = $this->propelConfiguration->getParameter('debugpdo.logging.outerglue', ' | ');
$innerGlue = $this->propelConfiguration->getParameter('debugpdo.logging.innerglue', ': ');
foreach($this->logger->getQueries() as $q)
{
$parts = explode($outerGlue, $q);
$times = explode($innerGlue, $parts[0]);
$memories = explode($innerGlue, $parts[1]);
$sql = trim($parts[2]);
$time = trim($times[1]);
$memory = trim($memories[1]);
$queries[] = new Query($sql, $time, $memory);
}
return $queries;
}
/**
* Count queries.
* @return int The number of queries.
*/
private function countQueries()
{
return count($this->logger->getQueries());
}
/**
* Returns queries.
*
* @return array Queries
*/
public function getQueries()
{
return $this->data['queries'];
}
/**
* Returns the query count.
*
* @return integer The query count
*/
public function getQueryCount()
{
return $this->data['querycount'];
}
}

View file

@ -1,81 +0,0 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\DataCollector;
/**
* A Query class is designed to represent query information.
*
* @author William DURAND <william.durand1@gmail.com>
*/
class Query
{
/**
* SQL statement
* @var string
*/
private $sql;
/**
* Execution time
*
* @var string
*/
private $time;
/**
* Memory
*
* @var string
*/
private $memory;
/**
* Default constructor
*
* @param $sql A SQL statement
* @param $time An execution time
* @param $memory Memory used
*/
public function __construct($sql, $time, $memory)
{
$this->sql = $sql;
$this->time = $time;
$this->memory = $memory;
}
/**
* Getter
*
* @return string SQL statement
*/
public function getSql()
{
return $this->sql;
}
/**
* Getter
*
* @return string Execution time
*/
public function getTime()
{
return $this->time;
}
/**
* Getter
*
* @return string Memory
*/
public function getMemory()
{
return $this->memory;
}
}

View file

@ -1,221 +0,0 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Form\ChoiceList;
use Symfony\Component\Form\Util\PropertyPath;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\ChoiceList\ArrayChoiceList;
/**
* Widely inspirated by the EntityChoiceList (Symfony2).
*
* @author William Durand <william.durand1@gmail.com>
*/
class ModelChoiceList extends ArrayChoiceList
{
/**
* The models from which the user can choose
*
* This array is either indexed by ID (if the ID is a single field)
* or by key in the choices array (if the ID consists of multiple fields)
*
* This property is initialized by initializeChoices(). It should only
* be accessed through getModel() and getModels().
*
* @var Collection
*/
private $models = array();
/**
* The fields of which the identifier of the underlying class consists
*
* This property should only be accessed through identifier.
*
* @var array
*/
private $identifier = array();
/**
* TableMap
*
* @var \TableMap
*/
private $table = null;
/**
* Property path
*
* @var \Symfony\Component\Form\Util\PropertyPath
*/
private $propertyPath = null;
/**
* Query
*/
private $query = null;
/**
* @param string $class
* @param string $property
* @param array $choices
* @param \ModelCriteria $queryObject
*/
public function __construct($class, $property = null, $choices = array(), $queryObject = null)
{
$this->class = $class;
$queryClass = $this->class . 'Query';
$query = new $queryClass();
$this->table = $query->getTableMap();
$this->identifier = $this->table->getPrimaryKeys();
$this->query = $queryObject ?: $query;
// The property option defines, which property (path) is used for
// displaying models as strings
if ($property) {
$this->propertyPath = new PropertyPath($property);
}
parent::__construct($choices);
}
/**
* Initializes the choices and returns them
*
* The choices are generated from the models. If the models have a
* composite identifier, the choices are indexed using ascending integers.
* Otherwise the identifiers are used as indices.
*
* If the models were passed in the "choices" option, this method
* does not have any significant overhead. Otherwise, if a query object
* was passed in the "query" option, this query is now used and executed.
* In the last case, all models for the underlying class are fetched.
*
* If the option "property" was passed, the property path in that option
* is used as option values. Otherwise this method tries to convert
* objects to strings using __toString().
*
* @return array An array of choices
*/
protected function load()
{
parent::load();
if ($this->choices) {
$models = $this->choices;
} else {
$models = $this->query->find();
}
$this->choices = array();
$this->models = array();
foreach ($models as $key => $model) {
if ($this->propertyPath) {
// If the property option was given, use it
$value = $this->propertyPath->getValue($model);
} else {
// Otherwise expect a __toString() method in the model
$value = (string)$model;
}
if (count($this->identifier) > 1) {
// When the identifier consists of multiple field, use
// naturally ordered keys to refer to the choices
$this->choices[$key] = $value;
$this->models[$key] = $model;
} else {
// When the identifier is a single field, index choices by
// model ID for performance reasons
$id = current($this->getIdentifierValues($model));
$this->choices[$id] = $value;
$this->models[$id] = $model;
}
}
}
public function getIdentifier()
{
return $this->identifier;
}
/**
* Returns the according models for the choices
*
* If the choices were not initialized, they are initialized now. This
* is an expensive operation, except if the models were passed in the
* "choices" option.
*
* @return array An array of models
*/
public function getModels()
{
if (!$this->loaded) {
$this->load();
}
return $this->models;
}
/**
* Returns the model for the given key
*
* If the underlying models have composite identifiers, the choices
* are intialized. The key is expected to be the index in the choices
* array in this case.
*
* If they have single identifiers, they are either fetched from the
* internal model cache (if filled) or loaded from the database.
*
* @param string $key The choice key (for models with composite
* identifiers) or model ID (for models with single
* identifiers)
* @return object The matching model
*/
public function getModel($key)
{
if (!$this->loaded) {
$this->load();
}
try {
if (count($this->identifier) > 1) {
// $key is a collection index
$models = $this->getModels();
return isset($models[$key]) ? $models[$key] : null;
} else if ($this->models) {
return isset($this->models[$key]) ? $this->models[$key] : null;
}
$queryClass = $this->class . 'Query';
return $queryClass::create()->findPk($key);
} catch (NoResultException $e) {
return null;
}
}
/**
* Returns the values of the identifier fields of an model
*
* Propel must know about this model, that is, the model must already
* be persisted or added to the idmodel map before. Otherwise an
* exception is thrown.
*
* @param object $model The model for which to get the identifier
* @throws FormException If the model does not exist
*/
public function getIdentifierValues($model)
{
if ($model instanceof \BaseObject) {
return array($model->getPrimaryKey());
}
return $model->getPrimaryKeys();
}
}

View file

@ -1,72 +0,0 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Form\DataTransformer;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\DataTransformerInterface;
use Propel\PropelBundle\Form\ChoiceList\ModelChoiceList;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class ModelToIdTransformer implements DataTransformerInterface
{
/**
* @var \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList
*/
private $choiceList;
/**
* @param \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList $choiceList
*/
public function __construct(ModelChoiceList $choiceList)
{
$this->choiceList = $choiceList;
}
public function transform($model)
{
if (null === $model || '' === $model) {
return '';
}
if (!is_object($model)) {
throw new UnexpectedTypeException($model, 'object');
}
if (count($this->choiceList->getIdentifier()) > 1) {
$availableModels = $this->choiceList->getModels();
return array_search($model, $availableModels);
}
return current($this->choiceList->getIdentifierValues($model));
}
public function reverseTransform($key)
{
if ('' === $key || null === $key) {
return null;
}
if (count($this->choiceList->getIdentifier()) > 1 && !is_numeric($key)) {
throw new UnexpectedTypeException($key, 'numeric');
}
if (!($model = $this->choiceList->getModel($key))) {
throw new TransformationFailedException(sprintf('The model with key "%s" could not be found', $key));
}
return $model;
}
}

View file

@ -1,98 +0,0 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Form\DataTransformer;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\DataTransformerInterface;
use \PropelCollection;
use \PropelObjectCollection;
use Propel\PropelBundle\Form\ChoiceList\ModelChoiceList;
/**
* ModelsToArrayTransformer class.
*
* @author William Durand <william.durand1@gmail.com>
* @author Pierre-Yves Lebecq <py.lebecq@gmail.com>
*/
class ModelsToArrayTransformer implements DataTransformerInterface
{
/**
* @var \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList
*/
private $choiceList;
/**
* @param \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList $choiceList
*/
public function __construct(ModelChoiceList $choiceList)
{
$this->choiceList = $choiceList;
}
public function transform($collection)
{
if (null === $collection) {
return array();
}
if (!$collection instanceof PropelCollection) {
throw new UnexpectedTypeException($collection, '\PropelCollection');
}
$array = array();
if (count($this->choiceList->getIdentifier()) > 1) {
$availableModels = $this->choiceList->getModels();
foreach ($collection as $model) {
$key = array_search($model, $availableModels);
$array[] = $key;
}
} else {
foreach ($collection as $model) {
$array[] = current($this->choiceList->getIdentifierValues($model));
}
}
return $array;
}
public function reverseTransform($keys)
{
$collection = new PropelObjectCollection();
if ('' === $keys || null === $keys) {
return $collection;
}
if (!is_array($keys)) {
throw new UnexpectedTypeException($keys, 'array');
}
$notFound = array();
foreach ($keys as $key) {
if ($model = $this->choiceList->getModel($key)) {
$collection->append($model);
} else {
$notFound[] = $key;
}
}
if (count($notFound) > 0) {
throw new TransformationFailedException(sprintf('The models with keys "%s" could not be found', implode('", "', $notFound)));
}
return $collection;
}
}

View file

@ -1,146 +0,0 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Form;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;
/**
* Propel Type guesser.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class PropelTypeGuesser implements FormTypeGuesserInterface
{
private $cache = array();
/**
* {@inheritDoc}
*/
public function guessType($class, $property)
{
if (!$table = $this->getTable($class)) {
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
}
foreach ($table->getRelations() as $relation) {
if (in_array($relation->getType(), array(\RelationMap::MANY_TO_ONE, \RelationMap::ONE_TO_MANY))) {
if ($property == $relation->getForeignTable()->getName()) {
return new TypeGuess('model', array(
'class' => $relation->getForeignTable()->getClassName(),
'multiple' => \RelationMap::MANY_TO_ONE === $relation->getType() ? false : true,
), Guess::HIGH_CONFIDENCE);
}
} elseif ($relation->getType() === \RelationMap::MANY_TO_MANY) {
if (strtolower($property) == strtolower($relation->getPluralName())) {
return new TypeGuess('model', array(
'class' => $relation->getLocalTable()->getClassName(),
'multiple' => true,
), Guess::HIGH_CONFIDENCE);
}
}
}
if (!$column = $this->getColumn($class, $property)) {
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
}
switch ($column->getType()) {
case \PropelColumnTypes::BOOLEAN:
case \PropelColumnTypes::BOOLEAN_EMU:
return new TypeGuess('checkbox', array(), Guess::HIGH_CONFIDENCE);
case \PropelColumnTypes::TIMESTAMP:
case \PropelColumnTypes::BU_TIMESTAMP:
return new TypeGuess('datetime', array(), Guess::HIGH_CONFIDENCE);
case \PropelColumnTypes::DATE:
case \PropelColumnTypes::BU_DATE:
return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE);
case \PropelColumnTypes::TIME:
return new TypeGuess('time', array(), Guess::HIGH_CONFIDENCE);
case \PropelColumnTypes::FLOAT:
case \PropelColumnTypes::REAL:
case \PropelColumnTypes::DOUBLE:
case \PropelColumnTypes::DECIMAL:
return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
case \PropelColumnTypes::TINYINT:
case \PropelColumnTypes::SMALLINT:
case \PropelColumnTypes::INTEGER:
case \PropelColumnTypes::BIGINT:
case \PropelColumnTypes::NUMERIC:
return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
case \PropelColumnTypes::CHAR:
case \PropelColumnTypes::VARCHAR:
return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE);
case \PropelColumnTypes::LONGVARCHAR:
case \PropelColumnTypes::BLOB:
case \PropelColumnTypes::CLOB:
case \PropelColumnTypes::CLOB_EMU:
return new TypeGuess('textarea', array(), Guess::MEDIUM_CONFIDENCE);
default:
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
}
}
/**
* {@inheritDoc}
*/
public function guessRequired($class, $property)
{
if ($column = $this->getColumn($class, $property)) {
return new ValueGuess($column->isNotNull(), Guess::HIGH_CONFIDENCE);
}
}
/**
* {@inheritDoc}
*/
public function guessMaxLength($class, $property)
{
if (($column = $this->getColumn($class, $property)) && $column->isText()) {
return new ValueGuess($column->getSize(), Guess::HIGH_CONFIDENCE);
}
}
/**
* {@inheritDoc}
*/
public function guessMinLength($class, $property)
{
}
protected function getTable($class)
{
if (isset($this->cache[$class])) {
return $this->cache[$class];
}
if (class_exists($queryClass = $class.'Query')) {
$query = new $queryClass();
return $this->cache[$class] = $query->getTableMap();
}
}
protected function getColumn($class, $property)
{
if (isset($this->cache[$class.'::'.$property])) {
return $this->cache[$class.'::'.$property];
}
$table = $this->getTable($class);
if ($table && $table->hasColumn($property)) {
return $this->cache[$class.'::'.$property] = $table->getColumn($property);
}
}
}

View file

@ -1,74 +0,0 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormFactoryInterface;
use Propel\PropelBundle\Form\ChoiceList\ModelChoiceList;
use Propel\PropelBundle\Form\DataTransformer\ModelToIdTransformer;
use Propel\PropelBundle\Form\DataTransformer\ModelsToArrayTransformer;
use Propel\PropelBundle\Form\EventListener\MergeCollectionListener;
/**
* ModelType class.
*
* @author William Durand <william.durand1@gmail.com>
*/
class ModelType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
if ($options['multiple']) {
$builder->prependClientTransformer(new ModelsToArrayTransformer($options['choice_list']));
} else {
$builder->prependClientTransformer(new ModelToIdTransformer($options['choice_list']));
}
}
public function getDefaultOptions(array $options)
{
$defaultOptions = array(
'template' => 'choice',
'multiple' => false,
'expanded' => false,
'class' => null,
'property' => null,
'query' => null,
'choices' => array(),
'preferred_choices' => array(),
);
$options = array_replace($defaultOptions, $options);
if (!isset($options['choice_list'])) {
$defaultOptions['choice_list'] = new ModelChoiceList(
$options['class'],
$options['property'],
$options['choices'],
$options['query']
);
}
return $defaultOptions;
}
public function getParent(array $options)
{
return 'choice';
}
public function getName()
{
return 'model';
}
}

View file

@ -1,137 +0,0 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Logger;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
/**
* PropelLogger.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author William DURAND <william.durand1@gmail.com>
*/
class PropelLogger
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var array
*/
protected $queries;
/**
* Constructor.
*
* @param LoggerInterface $logger A LoggerInterface instance
*/
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
$this->queries = array();
}
/**
* A convenience function for logging an alert event.
*
* @param mixed $message the message to log.
*/
public function alert($message)
{
if (null !== $this->logger) {
$this->logger->alert($message);
}
}
/**
* A convenience function for logging a critical event.
*
* @param mixed $message the message to log.
*/
public function crit($message)
{
if (null !== $this->logger) {
$this->logger->crit($message);
}
}
/**
* A convenience function for logging an error event.
*
* @param mixed $message the message to log.
*/
public function err($message)
{
if (null !== $this->logger) {
$this->logger->err($message);
}
}
/**
* A convenience function for logging a warning event.
*
* @param mixed $message the message to log.
*/
public function warning($message)
{
if (null !== $this->logger) {
$this->logger->warn($message);
}
}
/**
* A convenience function for logging an critical event.
*
* @param mixed $message the message to log.
*/
public function notice($message)
{
if (null !== $this->logger) {
$this->logger->notice($message);
}
}
/**
* A convenience function for logging an critical event.
*
* @param mixed $message the message to log.
*/
public function info($message)
{
if (null !== $this->logger) {
$this->logger->info($message);
}
}
/**
* A convenience function for logging a debug event.
*
* @param mixed $message the message to log.
*/
public function debug($message)
{
$this->queries[] = $message;
if (null !== $this->logger) {
$this->logger->debug($message);
}
}
/**
* Returns queries.
*
* @return array Queries
*/
public function getQueries()
{
return $this->queries;
}
}

View file

@ -7,12 +7,12 @@
<parameters>
<parameter key="propel.dbal.default_connection">default</parameter>
<parameter key="propel.configuration.class">PropelConfiguration</parameter>
<parameter key="propel.logger.class">Propel\PropelBundle\Logger\PropelLogger</parameter>
<parameter key="propel.data_collector.class">Propel\PropelBundle\DataCollector\PropelDataCollector</parameter>
<parameter key="propel.logger.class">Symfony\Bridge\Propel1\Logger\PropelLogger</parameter>
<parameter key="propel.data_collector.class">Symfony\Bridge\Propel1\DataCollector\PropelDataCollector</parameter>
<parameter key="propel.build_properties.class">Propel\PropelBundle\DependencyInjection\Properties</parameter>
<parameter key="propel.form.type.model.class">Propel\PropelBundle\Form\Type\ModelType</parameter>
<parameter key="propel.form.type.model.class">Symfony\Bridge\Propel1\Form\Type\ModelType</parameter>
<parameter key="propel.twig.extension.syntax.class">Propel\PropelBundle\Twig\Extension\SyntaxExtension</parameter>
<parameter key="form.type_guesser.propel.class">Propel\PropelBundle\Form\PropelTypeGuesser</parameter>
<parameter key="form.type_guesser.propel.class">Symfony\Bridge\Propel1\Form\PropelTypeGuesser</parameter>
</parameters>
<services>