Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ TypehintableBehavior

The **TypehintableBehavior** behavior allows you to add typehints to generated methods (in _Base_ classes).

_This is experimental branch with support for the upcoming Propel2._


Installation
------------

Cherry-pick the `TypehintableBehavior.php` file is `src/`, put it somewhere,
then add the following line to your `propel.ini` or `build.properties` configuration file:
The easiest way is through [Composer](https://github.com/composer/composer). Just add the following requirement to your project's `composer.json`:

``` json
{
"require": {
"willdurand/propel-typehintable-behavior": "~2.0@dev"
}
}
```

Then call

``` ini
propel.behavior.typehintable.class = path.to.TypehintableBehavior
``` bash
$ php composer.phar install
```


Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
"email": "[email protected]"
}
],
"type" : "propel-behavior",
"extra": {
"name": "typehintable",
"class": "TypehintableBehavior"
},
"require": {
"propel/propel1": "~1.6"
"propel/propel": "2.0.*@dev"
},
"autoload": {
"classmap": ["src/"]
Expand Down
17 changes: 12 additions & 5 deletions src/TypehintableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
*
* @license MIT License
*/
use Propel\Generator\Builder\Om\ObjectBuilder;
use Propel\Generator\Model\Behavior;

/**
* @author William Durand <[email protected]>
*/
class TypehintableBehavior extends Behavior
{
protected $parameters = array(
'nullable_columns' => null,
);

private $refFKs = array();

private $crossFKs = array();
Expand All @@ -23,16 +29,16 @@ class TypehintableBehavior extends Behavior

private $scalars = array('array', 'callable');

public function objectMethods($builder)
public function objectMethods(ObjectBuilder $builder)
{
if (null !== $this->getParameter('nullable_columns')) {
foreach (explode(',', $this->getParameter('nullable_columns')) as $column) {
$this->nullables[] = trim($column);
}

unset($this->parameters['nullable_columns']);
}

unset($this->parameters['nullable_columns']);

foreach ($this->getParameters() as $class) {
if (!in_array($class, $this->scalars)) {
$builder->declareClass($class);
Expand All @@ -51,8 +57,9 @@ public function objectFilter(&$script)
}

foreach ($this->getTable()->getCrossFks() as $fkList) {
list($refFK, $crossFK) = $fkList;
$this->crossFKs[$crossFK->getForeignTable()->getName()] = $crossFK->getRefPhpName() ?: $crossFK->getForeignTable()->getPhpName();
foreach ($fkList->getCrossForeignKeys() as $crossFK) {
$this->crossFKs[$crossFK->getForeignTable()->getName()] = $crossFK->getRefPhpName() ?: $crossFK->getForeignTable()->getPhpName();
}
}

foreach ($this->getTable()->getForeignKeys() as $fk) {
Expand Down
14 changes: 5 additions & 9 deletions tests/TypehintableBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class TypehintableBehaviorTest extends \PHPUnit_Framework_TestCase
{
private $schema;
private $con;

public function setUp()
{
Expand All @@ -38,7 +39,7 @@ public function setUp()
<column name="foo" type="OBJECT" />

<behavior name="typehintable">
<parameter name="typehinted_group" value="BaseTypehintedGroup" />
<parameter name="typehinted_group" value="TypehintedGroup" />
<parameter name="catched_exception" value="Exception" />
<parameter name="foo" value="TypehintedUser" />

Expand Down Expand Up @@ -66,12 +67,7 @@ public function setUp()
EOF;

if (!class_exists('TypehintedObject')) {
$builder = new PropelQuickBuilder();
$config = $builder->getConfig();
$config->setBuildProperty('behavior.typehintable.class', __DIR__.'/../src/TypehintableBehavior');
$builder->setConfig($config);
$builder->setSchema($this->schema);
$builder->build();
$this->con = \Propel\Generator\Util\QuickBuilder::buildSchema($this->schema);
}
}

Expand Down Expand Up @@ -134,7 +130,7 @@ public function testTypehintOnManyToManyRelationRemover()
$ref = new ReflectionClass('TypehintedUser');
$parameters = $ref->getMethod('removeTypehintedGroup')->getParameters();

$this->assertEquals('BaseTypehintedGroup', $parameters[0]->getClass()->getName());
$this->assertEquals('Base\TypehintedGroup', $parameters[0]->getClass()->getName());
}

public function testTypehintIsNullable()
Expand All @@ -156,6 +152,6 @@ public function testSchemaIsValid()
$xml = new DOMDocument();
$xml->loadXML($this->schema);

$this->assertTrue($xml->schemaValidate(__DIR__ . '/../vendor/propel/propel1/generator/resources/xsd/database.xsd'));
$this->assertTrue($xml->schemaValidate(__DIR__ . '/../vendor/propel/propel/resources/xsd/database.xsd'));
}
}
3 changes: 1 addition & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';
set_include_path(__DIR__ . '/../vendor/phing/phing/classes' . PATH_SEPARATOR . get_include_path());

require_once __DIR__ . '/../vendor/propel/propel1/generator/lib/util/PropelQuickBuilder.php';
require_once __DIR__ . '/../vendor/propel/propel/src/Propel/Generator/Util/QuickBuilder.php';