Permalink
Browse files

Install Behaviors with Composer

  • Loading branch information...
1 parent 793aea6 commit 8b5fe571067bac9951033521e5f47332342a7cef @gossi gossi committed Jan 7, 2014
View
@@ -1,6 +1,6 @@
vendor/
composer.phar
-composer.lock
+/composer.lock
autoload.php
phpunit.xml
pom.xml
@@ -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>
@@ -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')
@@ -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'),
@@ -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
@@ -39,6 +40,11 @@ class GeneratorConfig implements GeneratorConfigInterface
protected $defaultBuildConnection = null;
/**
+ * @var BehaviorLocator
+ */
+ protected $behaviorLocator = null;
+
+ /**
* Construct a new GeneratorConfig.
*
* @param array|Traversable $props
@@ -327,4 +333,13 @@ public function getConnection($database)
return $con;
}
+
+ public function getBehaviorLocator() {
+ if (!$this->behaviorLocator) {
+ $this->behaviorLocator = new BehaviorLocator($this);
+ }
+
+ return $this->behaviorLocator;
+ }
+
}
@@ -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
{
@@ -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();
}
@@ -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
{
@@ -33,6 +34,11 @@ class QuickGeneratorConfig implements GeneratorConfigInterface
);
protected $buildProperties = array();
+
+ /**
+ * @var BehaviorLocator
+ */
+ protected $behaviorLocator = null;
public function __construct()
{
@@ -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;
+ }
}
@@ -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;
+ }
+}
@@ -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.
@@ -27,6 +30,9 @@
*/
class Database extends ScopedMappingModel
{
+
+ use BehaviorableTrait;
+
/**
* The database's platform.
*
@@ -89,7 +95,6 @@ class Database extends ScopedMappingModel
*/
private $sequences;
- protected $behaviors;
protected $defaultStringFormat;
protected $tablePrefix;
@@ -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.
*
@@ -837,6 +785,11 @@ public function doFinalInitialization()
$table->setupReferrers(true);
}
}
+
+ protected function registerBehavior(Behavior $behavior)
+ {
+ $behavior->setDatabase($this);
+ }
/**
* Setups all table referrers.
@@ -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;
- }
}
Oops, something went wrong.

0 comments on commit 8b5fe57

Please sign in to comment.