Skip to content

Commit

Permalink
add BaseDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanbarlog committed Oct 24, 2017
1 parent 8399988 commit f8e8a8d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,49 @@ class CustomForm extends AbstractType {
}
```

# Reusing data sources

In order to reuse data source between for instance multiple tabs you can easily create Twig functions by extending our `BaseDataSource`.

Simply add to your services.yml following statement:

```yaml
AppBundle\DataProvider\:
resource: '../../src/AppBundle/DataProvider'
tags: ['twig.extension']
```

You can specify any folder within your project you want. In this example we have chosen `AppBundle\DataProvider` namespace.

Each class within this namespace which extends `Everlution\AjaxcomBundle\DataSource\BaseDataSource` is scanned for public methods with suffix `Provider` via reflexion and we are creating the simple Twig functions from these methods. Let's see the example:


```php
// AppBundle\DataProvider\Example.php
// simple function which returns static array
public function navigationProvider() {
return [
// some data...
];
}
// you can use parametrical functions and injected services as well
public function userProfileProvider(int $id) {
return $this->someService->getData($id);
}
```

After creating such class you can simply call the function within twig:

```twig
{{ dump(navigation()); }} {# will dump static array #}
{% for item in userProfile(2) %}
{{ dump(item) }}
{% endfor %}
```

# Best practice

If you want to use AjaxcomBundle seamlessly you should copy `@EverlutionAjaxcom\layout_bootstrap_4.html.twig` to your project (eg. AppBundle) and modify it to your needs.
Expand Down
43 changes: 43 additions & 0 deletions src/DataSource/BaseDataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Everlution\AjaxcomBundle\DataSource;

/**
* Class BaseDataSource.
*
* @author Ivan Barlog <[email protected]>
*/
class BaseDataSource extends \Twig_Extension
{
protected const TWIG_FUNCTION_SUFFIX = 'Provider';

/**
* @return array
*/
public function getFunctions()
{
$functions = [];
$class = new \ReflectionClass(get_class($this));

if (self::class === $class->name) {
return $functions;
}

foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
if (false === ($offset = strpos($method->getName(), self::TWIG_FUNCTION_SUFFIX))) {
continue;
}

$functions[] = $this->registerAsTwigFunction(substr($method->getName(), 0, $offset));
}

return $functions;
}

private function registerAsTwigFunction(string $name): ?\Twig_SimpleFunction
{
return new \Twig_SimpleFunction($name, [$this, $name.self::TWIG_FUNCTION_SUFFIX]);
}
}

0 comments on commit f8e8a8d

Please sign in to comment.