Skip to content

Commit

Permalink
Add behavior parsing for table mixin annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Mar 12, 2017
1 parent 01f2ba4 commit e53269e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/Annotator/EntityAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public function annotate($path) {
}

$content = file_get_contents($path);
if (preg_match('/\* @property .+ \$/', $content)) {
return null;
}

$helper = new DocBlockHelper(new View());
$schema = $this->getConfig('schema');
Expand All @@ -33,6 +30,12 @@ public function annotate($path) {
$annotations = array_merge($annotations, $helper->propertyHints($associationHintMap));
}

foreach ($annotations as $key => $annotation) {
if (preg_match('/' . preg_quote($annotation) . '/', $content)) {
unset($annotations[$key]);
}
}

return $this->_annotate($path, $content, $annotations);
}

Expand Down
31 changes: 27 additions & 4 deletions src/Annotator/ModelAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ protected function _table($path, $entityName, array $associations) {

$entity = $entityName;

//TODO
$behaviors = [];
$behaviors = $this->_parseLoadedBehaviors($content);

$namespace = $this->getConfig(static::CONFIG_NAMESPACE);
$annotations = [];
Expand All @@ -85,8 +84,17 @@ protected function _table($path, $entityName, array $associations) {
$annotations[] = "@method \\{$namespace}\\Model\\Entity\\{$entity} patchEntity(\\Cake\\Datasource\\EntityInterface \$entity, array \$data, array \$options = [])";
$annotations[] = "@method \\{$namespace}\\Model\\Entity\\{$entity}[] patchEntities(\$entities, array \$data, array \$options = [])";
$annotations[] = "@method \\{$namespace}\\Model\\Entity\\{$entity} findOrCreate(\$search, callable \$callback = null, \$options = [])";
foreach ($behaviors as $behavior => $behaviorData) {
$annotations[] = "@mixin \\Cake\\ORM\\Behavior\\{$behavior}Behavior";

foreach ($behaviors as $behavior) {
$className = App::className($behavior, 'Model/Behavior', 'Behavior');
if (!$className) {
$className = App::className($behavior, 'ORM/Behavior', 'Behavior');
}
if (!$className) {
continue;
}

$annotations[] = "@mixin \\{$className}";
}

foreach ($annotations as $key => $annotation) {
Expand Down Expand Up @@ -126,4 +134,19 @@ protected function _entity($entityName, array $schema) {
return true;
}

/**
* @param string $content
* @return array
*/
protected function _parseLoadedBehaviors($content) {
preg_match_all('/\$this-\>addBehavior\(\'([a-z.]+)\'/i', $content, $matches);
if (empty($matches)) {
return [];
}

$behaviors = $matches[1];

return array_unique($behaviors);
}

}
2 changes: 1 addition & 1 deletion tests/TestCase/Annotator/ModelAnnotatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testAnnotate() {

$output = (string)$this->out->output();

$this->assertTextContains(' * 7 annotations added', $output);
$this->assertTextContains(' * 9 annotations added', $output);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/test_app/src/Model/Table/FooTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@
use Cake\ORM\Table;

class FooTable extends Table {

/**
* @param array $config
* @return void
*/
public function initialize(array $config) {
parent::initialize($config);

$this->addBehavior('Tools.Confirmable');
$this->addBehavior('Timestamp');
}

}
15 changes: 15 additions & 0 deletions tests/test_files/Model/Table/FooTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@
* @method \App\Model\Entity\Foo patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\Foo[] patchEntities($entities, array $data, array $options = [])
* @method \App\Model\Entity\Foo findOrCreate($search, callable $callback = null, $options = [])
*
* @mixin \Tools\Model\Behavior\ConfirmableBehavior
* @mixin \Cake\ORM\Behavior\TimestampBehavior
*/
class FooTable extends Table {

/**
* @param array $config
* @return void
*/
public function initialize(array $config) {
parent::initialize($config);

$this->addBehavior('Tools.Confirmable');
$this->addBehavior('Timestamp');
}

}

0 comments on commit e53269e

Please sign in to comment.