Skip to content

Commit

Permalink
Merge pull request #3 from ahmetcelikezer/2.0.0
Browse files Browse the repository at this point in the history
2.0.0
  • Loading branch information
ahmetcelikezer authored Sep 27, 2019
2 parents 38cd30d + 3a1db91 commit ab7f68f
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 22 deletions.
24 changes: 22 additions & 2 deletions DependencyInjection/AhcTwigSeoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,40 @@

class AhcTwigSeoExtension extends Extension implements PrependExtensionInterface
{
/**
* @var array
*/
private $preLoadedConfigs;

public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration(new Configuration(), $configs);

$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');

$config['groups'] = $this->overwriteDefaultConfigs($config);
$container->setParameter('ahc_twig_seo_config', $config);
}

public function prepend(ContainerBuilder $container)
{
$config = Yaml::parseFile(__DIR__.'/../Resources/config/ahc_twig_seo.yaml');
$configs = Yaml::parseFile(__DIR__.'/../Resources/config/ahc_twig_seo.yaml');
$this->preLoadedConfigs = $configs['ahc_twig_seo'];
}

private function overwriteDefaultConfigs(array $configs): array
{
$defaultGroups = array_keys($this->preLoadedConfigs['groups']);
$customGroups = array_keys($configs['groups']);

foreach ($customGroups as $customGroup) {
if(in_array($customGroup, $defaultGroups)) {
unset($this->preLoadedConfigs['groups'][$customGroup]);
}
}

return array_merge($this->preLoadedConfigs['groups'], $configs['groups']);

$container->prependExtensionConfig('ahc_twig_seo', $config['ahc_twig_seo']);
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ composer require ahc/twigseobundle

## Documentation
* Versions
* [Latest (1.0.0)](https://github.com/ahmetcelikezer/twig-seo-bundle/tree/master/Resources/doc/latest/README.md)
* [Latest (2.0.0)](https://github.com/ahmetcelikezer/twig-seo-bundle/tree/master/Resources/doc/latest/README.md)
* [1.0.0/1/2](https://github.com/ahmetcelikezer/twig-seo-bundle/blob/master/Resources/doc/v1.0.0/README.md)
* [0.1.0](https://github.com/ahmetcelikezer/twig-seo-bundle/blob/master/Resources/doc/v0.1.0/README.md)

## Next Version Planned Features
Expand Down
7 changes: 2 additions & 5 deletions Resources/doc/latest/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Twig Seo Bundle v1.0.0

> This version is a major and version and is not supported by the older version(s)!
# Twig Seo Bundle v2.0.0

This extension bundle is designed for automating to printing meta tags. You can use default meta tag groups defined by us or you can create your very own element groups with unlimited parameters to print them to your twig templates.
## Installation
Expand Down Expand Up @@ -219,5 +217,4 @@ Default Tags the Group Has:
## Next Version Planned Features
1. Loop able arguments for same tag(s).


2. More options and methods support for groups.
225 changes: 225 additions & 0 deletions Resources/doc/v1.0.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
# Twig Seo Bundle v1.0.0 Not Usable !!!

> Version 1 is buggy and not usable, use Version 2.0 instead!
> This version is a major and version and is not supported by the older version(s)!
This extension bundle is designed for automating to printing meta tags. You can use default meta tag groups defined by us or you can create your very own element groups with unlimited parameters to print them to your twig templates.
## Installation
You can install this bundle with composer.
```bash
composer require ahc/twigseobundle
```

## Instructions
- [Creating a Element Groups](#creating-custom-element-groups)
- [Defining Element Groups](#an-example-to-create-custom-element-group)
- [Calling Other Groups](#methods)
- [Default Element Groups](#default-element-groups)
- [Base Elements](#base)
- [Title Elements](#title)
- [Description Elements](#description)
- [Og Image Elements](#og-image)

## Creating Custom Element Groups
With this bundle, creating your own element groups to print with `seo_group` method. You can also customize the default element groups.

The meta groups can be defined under `ahc_twig_seo > groups` namespace in `ahc_twig_seo.yaml` file.

You can use these keys when you define or edit an meta group:

| Key | Explanation | Type | Required |
| ------------- | ---------------------------------------------------------- | ----- | --------- |
| arguments | Determines the variables of the parameters in the tag. | array | false |
| tags | Contains the elements of group has. | array | false |
| methods | Methods to call for print other defined or default groups. | array | false |

### Group Keys
The keys that groups can contain. I will be explain each key over the example configuration.

Example Configuration:
```yaml
ahc_twig_seo:
groups:
# Our meta group
example_group:
arguments:
title: '%%title%%'
description: '%%description%%'
imagePath: '%%image%%'
tags:
- '<title>%%title%%</title>'
- '<meta property="og:site_name" content="%%title%%" />'
- '<meta property="og:title" content="%%title%%" />'
- '<meta name="twitter:title" content="%%title%%" />'
methods: ['default_description', 'favicon_group']

# Defined meta group
favicon_group:
arguments:
imagePath: '%%image%%'
tags:
- '<meta property="og:image" content="%%image_path%%" />'
- '<meta name="twitter:image" content="%%image_path%%" />'
- '<link rel="image_src" href="%%image_path%%" />'

# Default meta group
default_description:
arguments:
description: '%%description%%'
tags:
- '<meta name="description" content="%%description%%" />'
- '<meta property="og:description" content="%%description%%" />'
- '<meta name="twitter:description" content="%%description%%" />'

```
##### arguments:
Keys are defined in this key contains parameters the variable replacement for variables the meta tags has. The key of array is used for set the data for it's value by twig method.

> Note: The key of value defined in arguments option have to be same with the twig method parameter.
For example, we want to print `favicon_group`, so we have to define the variable(s) in our group argument(s).
Favicon group only requires `imagePath` parameter. We can call it like:

```twig
seo_group('favicon_group', { imagePath: 'path/of/image.jpg' })
```

Now all the tags which is contains `%%image%%` key will be replaced the `path/of/image.jpg` value.

output:
```html
<link rel="image_src" href="path/of/image.jpg" />
<meta name="twitter:image" content="path/of/image.jpg" />
<link rel="image_src" href="path/of/image.jpg" />
```

###### an example to create custom element group:
you can define unlimited meta groups like that.

```yaml
ahc_twig_seo: # Root namespace for configuration of bundle.
groups: # Namespace to define meta groups.

example_group: # Name that you want to set for the meta group.
arguments: # Parameters array for the set to meta group.
size: '%%size%%'
image_path: '%%image_path%%'
mime_type: '%%ext%%'
tags: # Meta tags the defined meta group will have.
- '<link rel="icon" type="%%ext%%" sizes="%%size%%" href="%%image_path%%">'

```
to print `example_group` defined like above, you can call it in your twig template:
```twig
seo_group('example_group', { size: '16x16', image_path: 'img/assets/img/favicon.png', mime_type:'image/png' })
```

output will be:
```html
<link rel="icon" type="image/png" sizes="16x16" href="img/assets/img/favicon.png">
```

##### tags:
The html tags array to print. Html tags can have a parameter(s) to replace. It should be defined between double percent signs `%%`. For example in the example configuration `%%description%%` will be replaced the data given `description` variable defined inside `arguments` in the configuration.

##### methods:
It stores the group names to run before the tags replacement. Defined methods will be printed, bu you should define all the arguments for the group(s) requires.

For Example:
```yaml
ahc_twig_seo:
groups:
test_group:
arguments:
title: '%%title%%'
description: '%%description%%'
methods: ['default_title', 'default_description']

```
The group must contain arguments which the defined methods requires. This test_group will call `default_title` and `default_description` groups with parameters it takes.

```twig
#seo_group([string]groupName, [json]arrayOfArguments)
seo_group('test_group', { title: 'Example Title', description: 'Example Description' })
```

### Default Element Groups:
You can use default groups faster with their methods defined. However you can also customize all data default group has.

To customize default groups, you can define them in `ahc_twig_seo.yaml` configuration using its group name just like you are defining brand new group but it will be overwritten with your customization.

##### Base
| Method Name | Group Name | Required Arguments |
| ------------- | ------------- |------------------- |
| seo_base | default_base | NULL |

```twig
seo_base()
```
This method will be print the group ```default_base```
Default Tags the Group Has:
```html
<meta charset="utf-8">
<meta property="og:type" content="website" />
<meta content="IE=edge" http-equiv="X-UA-Compatible" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
```
---
##### Title
| Method Name | Group Name | Required Arguments |
| ------------- | ------------- |------------------- |
| seo_title | default_title | (string)title |

```twig
seo_title('Example Title')
```
This method will be print the group ```default_title```
Default Tags the Group Has:
```html
<title>%%title%%</title>
<meta property="og:site_name" content="%%title%%">
<meta property="og:title" content="%%title%%" />
<meta name="twitter:title" content="%%title%%" />
```
> The parameters between the `%%` will be replaced with the given parameter in method.
---
##### Description
| Method Name | Group Name | Required Arguments |
| --------------- | -------------------- |-------------------- |
| seo_description | default_description | (string)description |

```twig
seo_description('Example Description')
```
This method will be print the group ```default_description```
Default Tags the Group Has:
```html
<meta name="description" content="%%description%%" />
<meta property="og:description" content="%%description%%" />
<meta name="twitter:description" content="%%description%%" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
```
> The parameters between the `%%` will be replaced with the given parameter in method.
---
##### Og Image
| Method Name | Group Name | Required Arguments |
| ------------- | ----------------- |------------------- |
| seo_image | default_og_images | (string)imagePath |

```twig
seo_image('absolute/path/to/img.jpg')
```
This method will be print the group ```default_og_images```
Default Tags the Group Has:
```html
<meta property="og:image" content="%%image_path%%" />
<meta name="twitter:image" content="%%image_path%%" />
<link rel="image_src" href="%%image_path%%" />
```
> The parameters between the `%%` will be replaced with the given parameter in method.
## Next Version Planned Features
1. Loop able arguments for same tag(s).


8 changes: 4 additions & 4 deletions Twig/AhcTwigSeoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public function printBase(): Markup

public function printSeoImage(string $imagePath): Markup
{
$titleElement = new Element('default_og_images');
$titleElement->setArgumentValues([
'image_path' => $imagePath,
$seoImageElement = new Element('default_og_images');
$seoImageElement->setArgumentValues([
'imagePath' => $imagePath,
]);

return $this->elementManager
->setElement($titleElement)
->setElement($seoImageElement)
->registerElementArguments()
->printElement()
;
Expand Down
13 changes: 13 additions & 0 deletions Utils/ConfigurationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function getGroups():array
public function getGroup(string $group): array
{
if(true === array_key_exists($group, $this->getGroups())){

This comment has been minimized.

Copy link
@ali-aslan-oztoprak

ali-aslan-oztoprak Oct 20, 2019

I encountered a problem last month with array_key_exist. The first parameter should be string or integer for the method. I mean, if you would check the first parameter according to this rule, it's would be good.


return $this->getGroups()[$group];
}

Expand All @@ -37,16 +38,28 @@ public function getGroup(string $group): array

public function getGroupArguments(string $group): array
{
if(!isset($this->getGroup($group)['arguments']) || null === $this->getGroup($group)['arguments']) {
return [];
}

return $this->getGroup($group)['arguments'];
}

public function getGroupTags(string $group): array
{
if(!isset($this->getGroup($group)['tags']) || null === $this->getGroup($group)['tags']) {
return [];
}

return $this->getGroup($group)['tags'];
}

public function getGroupMethods(string $group): array
{
if(!isset($this->getGroup($group)['methods']) || null === $this->getGroup($group)['methods']) {
return [];
}

return $this->getGroup($group)['methods'];
}
}
12 changes: 2 additions & 10 deletions Utils/ElementManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function setElement(Element $element, bool $autoGetTags = true): self
return $this;
}

// TODO: Parse this function as html converter from tags[array], arguments[array], argumentValues[array]
public function registerElementArguments(): self
{
$convertedTags = [];
Expand All @@ -54,9 +53,7 @@ public function registerElementArguments(): self
$methods = $this->element->getMethods();

if (count($methods) > 0) {
// TODO: Make it reverse if its not work!
//dd($convertedTags);
//dd($this->getSubContents());

array_push($convertedTags, $this->getSubContents(true));
}

Expand Down Expand Up @@ -91,12 +88,7 @@ public function getElementContent()
$html = '';

foreach ($this->element->getContentArray() as $key=>$content) {
try{
$html .= $content;
} catch (\Exception $e) {
dd($content);
}

$html .= $content;
}

return $html;
Expand Down

0 comments on commit ab7f68f

Please sign in to comment.