Skip to content

Commit

Permalink
Install Behaviors with Composer
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi committed Jan 29, 2014
1 parent 793aea6 commit 8b5fe57
Show file tree
Hide file tree
Showing 13 changed files with 972 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vendor/
composer.phar
composer.lock
/composer.lock
autoload.php
phpunit.xml
pom.xml
Expand Down
5 changes: 5 additions & 0 deletions src/Propel/Generator/Command/ModelBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Propel\Generator\Manager\ModelManager;
use Propel\Runtime\Propel;
use Propel\Generator\Util\ComposerFinder;

/**
* @author Florian Klein <florian.klein@free.fr>
Expand Down Expand Up @@ -80,6 +82,8 @@ protected function configure()
'')
->addOption('disable-namespace-auto-package', null, InputOption::VALUE_NONE,
'Disable namespace auto-packaging')
->addOption('composer-dir', null, InputOption::VALUE_REQUIRED,
'Directory in which your composer.json resides', null)
->setName('model:build')
->setAliases(array('build'))
->setDescription('Build the model classes based on Propel XML schemas')
Expand All @@ -102,6 +106,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'propel.builder.queryinheritancestub.class' => $input->getOption('query-inheritance-stub-class'),
'propel.builder.tablemap.class' => $input->getOption('tablemap-class'),
'propel.builder.pluralizer.class' => $input->getOption('pluralizer-class'),
'propel.builder.composer.dir' => $input->getOption('composer-dir'),
'propel.disableIdentifierQuoting' => !$input->getOption('enable-identifier-quoting'),
'propel.targetPackage' => $input->getOption('target-package'),
'propel.packageObjectModel' => $input->getOption('enable-package-object-model'),
Expand Down
15 changes: 15 additions & 0 deletions src/Propel/Generator/Config/GeneratorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Propel\Runtime\Adapter\AdapterFactory;
use Propel\Runtime\Connection\ConnectionFactory;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Generator\Util\BehaviorLocator;

/**
* A class that holds build properties and provide a class loading mechanism for
Expand All @@ -38,6 +39,11 @@ class GeneratorConfig implements GeneratorConfigInterface

protected $defaultBuildConnection = null;

/**
* @var BehaviorLocator
*/
protected $behaviorLocator = null;

/**
* Construct a new GeneratorConfig.
*
Expand Down Expand Up @@ -327,4 +333,13 @@ public function getConnection($database)

return $con;
}

public function getBehaviorLocator() {
if (!$this->behaviorLocator) {
$this->behaviorLocator = new BehaviorLocator($this);
}

return $this->behaviorLocator;
}

}
8 changes: 8 additions & 0 deletions src/Propel/Generator/Config/GeneratorConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Propel\Generator\Model\Table;
use Propel\Generator\Platform\PlatformInterface;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Generator\Util\BehaviorLocator;

interface GeneratorConfigInterface
{
Expand Down Expand Up @@ -59,4 +60,11 @@ public function setBuildProperty($name, $value);
* @return PlatformInterface
*/
public function getConfiguredPlatform(ConnectionInterface $con = null, $database = null);

/**
* Returns the behavior locator.
*
* @return BehaviorLocator
*/
public function getBehaviorLocator();
}
15 changes: 15 additions & 0 deletions src/Propel/Generator/Config/QuickGeneratorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Propel\Generator\Exception\RuntimeException;
use Propel\Generator\Model\Table;
use \Propel\Runtime\Connection\ConnectionInterface;
use Propel\Generator\Util\BehaviorLocator;

class QuickGeneratorConfig implements GeneratorConfigInterface
{
Expand All @@ -33,6 +34,11 @@ class QuickGeneratorConfig implements GeneratorConfigInterface
);

protected $buildProperties = array();

/**
* @var BehaviorLocator
*/
protected $behaviorLocator = null;

public function __construct()
{
Expand Down Expand Up @@ -154,4 +160,13 @@ public function getConfiguredPlatform(ConnectionInterface $con = null, $database
{
return null;
}


public function getBehaviorLocator() {
if (!$this->behaviorLocator) {
$this->behaviorLocator = new BehaviorLocator($this);
}

return $this->behaviorLocator;
}
}
116 changes: 116 additions & 0 deletions src/Propel/Generator/Model/BehaviorableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
namespace Propel\Generator\Model;

use Propel\Generator\Util\BehaviorLocator;
use Propel\Generator\Exception\BuildException;
use Propel\Generator\Config\GeneratorConfigInterface;

/**
* BehaviorableTrait use it on every model that can hold behaviors
*
*/
trait BehaviorableTrait
{
/**
* @var Behavior[]
*/
protected $behaviors;

/**
* @var BehaviorLocator
*/
private $behaviorLocator;

/**
* @return GeneratorConfigInterface
*/
protected abstract function getGeneratorConfig();

/**
* Returns the behavior locator.
*
* @return BehaviorLocator
*/
private function getBehaviorLocator()
{
if (null === $this->behaviorLocator) {
$config = $this->getGeneratorConfig();
if (null !== $config) {
$this->behaviorLocator = $config->getBehaviorLocator();
if (null === $this->behaviorLocator) {
$this->behaviorLocator = new BehaviorLocator();
}
} else {
$this->behaviorLocator = new BehaviorLocator();
}
}

return $this->behaviorLocator;
}

/**
* Adds a new Behavior
*
* @param $bdata
* @throws BuildException when the added behavior is not an instance of \Propel\Generator\Model\Behavior
* @return Behavior $bdata
*/
public function addBehavior($bdata)
{
if ($bdata instanceof Behavior) {
$behavior = $bdata;
$this->registerBehavior($behavior);
$this->behaviors[$behavior->getName()] = $behavior;

return $behavior;
}

$locator = $this->getBehaviorLocator();
$class = $locator->getBehavior($bdata['name']);
$behavior = new $class();
if (!($behavior instanceof Behavior)) {
throw new BuildException(sprintf('Behavior [%s: %s] not instance of %s',
$bdata['name'], $class, '\Propel\Generator\Model\Behavior'));
}
$behavior->loadMapping($bdata);

return $this->addBehavior($behavior);
}

protected abstract function registerBehavior(Behavior $behavior);

/**
* Returns the list of behaviors.
*
* @return Behavior[]
*/
public function getBehaviors()
{
return $this->behaviors;
}

/**
* check if the given behavior exists
*
* @param string $name the behavior name
* @return boolean True if the behavior exists
*/
public function hasBehavior($name)
{
return array_key_exists($name, $this->behaviors);
}

/**
* Get behavior by name
*
* @param string $name the behavior name
* @return Behavior a behavior object or null if the behavior doesn't exist
*/
public function getBehavior($name)
{
if ($this->hasBehavior($name)) {
return $this->behaviors[$name];
}
return null;
}
}
71 changes: 12 additions & 59 deletions src/Propel/Generator/Model/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Propel\Generator\Exception\EngineException;
use Propel\Generator\Exception\InvalidArgumentException;
use Propel\Generator\Platform\PlatformInterface;
use Propel\Generator\Util\BehaviorLocator;
use Propel\Generator\Config\QuickGeneratorConfig;
use Propel\Generator\Exception\BuildException;

/**
* A class for holding application data structures.
Expand All @@ -27,6 +30,9 @@
*/
class Database extends ScopedMappingModel
{

use BehaviorableTrait;

/**
* The database's platform.
*
Expand Down Expand Up @@ -89,7 +95,6 @@ class Database extends ScopedMappingModel
*/
private $sequences;

protected $behaviors;
protected $defaultStringFormat;
protected $tablePrefix;

Expand Down Expand Up @@ -697,64 +702,7 @@ public function getBuildProperty($name)
return $config->getBuildProperty($name);
}
}

/**
* Adds a new behavior to the database*
*
* @param Behavior|array $bdata
* @return Behavior
*/
public function addBehavior($bdata)
{
if ($bdata instanceof Behavior) {
$behavior = $bdata;
$behavior->setDatabase($this);
$this->behaviors[$behavior->getName()] = $behavior;

return $behavior;
}

$class = $this->getConfiguredBehavior($bdata['name']);
$behavior = new $class();
$behavior->loadMapping($bdata);

return $this->addBehavior($behavior);
}

/**
* Returns the list of all database behaviors.
*
* @return array
*/
public function getBehaviors()
{
return $this->behaviors;
}

/**
* Returns whether or not the database has a specific behavior.
*
* @param string $name
* @return boolean
*/
public function hasBehavior($name)
{
return isset($this->behaviors[$name]);
}

/**
* Returns the corresponding behavior identified by its name.
*
* @param string $name
* @return Behavior
*/
public function getBehavior($name)
{
if (isset($this->behaviors[$name])) {
return $this->behaviors[$name];
}
}


/**
* Returns the table prefix for this database.
*
Expand Down Expand Up @@ -837,6 +785,11 @@ public function doFinalInitialization()
$table->setupReferrers(true);
}
}

protected function registerBehavior(Behavior $behavior)
{
$behavior->setDatabase($this);
}

/**
* Setups all table referrers.
Expand Down
25 changes: 0 additions & 25 deletions src/Propel/Generator/Model/MappingModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,29 +182,4 @@ public function getVendorInformation()
return $this->vendorInfos;
}

/**
* Returns the best class name for a given behavior.
*
* If not found, the method tries to autoload \Propel\Generator\Behavior\[Bname]\[Bname]Behavior
*
* @param string $behavior The behavior name (ie: timestampable)
* @return string $class The behavior fully qualified class name
* @throws BehaviorNotFoundException
*/
public function getConfiguredBehavior($behavior)
{
if (false !== strpos($behavior, '\\')) {
$class = $behavior;
} else {
$generator = new PhpNameGenerator();
$phpName = $generator->generateName([ $behavior, PhpNameGenerator::CONV_METHOD_PHPNAME ]);
$class = sprintf('\\Propel\\Generator\\Behavior\\%s\\%sBehavior', $phpName, $phpName);
}

if (!class_exists($class)) {
throw new BehaviorNotFoundException(sprintf('Unknown behavior "%s"', $behavior));
}

return $class;
}
}
Loading

0 comments on commit 8b5fe57

Please sign in to comment.