Skip to content

Commit e3d804a

Browse files
committed
Initial commit
0 parents  commit e3d804a

File tree

12 files changed

+222
-0
lines changed

12 files changed

+222
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
composer.lock

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Code Rhapsodie
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Get Url Contents eZ Twig Bundme
2+
3+
This bundles adds:
4+
5+
- a `get_url_contents` Twig function
6+
- a `get_url_contents` eZ Publish legacy template operator
7+
8+
Both share the same behaviour:
9+
10+
- perform a GET HTTP request to the URL passed as argument, using curl
11+
- then, if the request succeeded and the response code is 200, return the response body as string
12+
- otherwise, return an empty string
13+
14+
## Install
15+
16+
Just use Composer:
17+
18+
`composer require code-rhapsodie/get-url-contents-ez-twig-bundle`
19+
20+
## Usage
21+
22+
In Twig:
23+
24+
```twig
25+
{{ get_url_contents('https://host/my_external_file.html') }}
26+
```
27+
28+
In eZ Publish legacy templates:
29+
30+
```
31+
{get_url_contents('https://host/my_external_file.html')}
32+
```

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "code-rhapsodie/get-url-contents-ez-twig-bundle",
3+
"description": "",
4+
"type": "symfony-bundle",
5+
"keywords": [],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Code Rhapsodie",
10+
"homepage": "https://www.code-rhapsodie.fr/"
11+
},
12+
{
13+
"name": "Other contributors",
14+
"homepage": "https://github.com/code-rhapsodie/get-url-contents-ez-twig-bundle/graphs/contributors"
15+
}
16+
],
17+
"autoload": {
18+
"psr-4": {
19+
"CodeRhapsodie\\GetUrlContentsEzTwigBundle\\": "src/"
20+
}
21+
},
22+
"require": {
23+
"php": "^7.0",
24+
"ext-curl": "*",
25+
"symfony/dependency-injection": "^3.4||^4.0||^5.0",
26+
"symfony/twig-bundle": "^3.4||^4.0||^5.0"
27+
},
28+
"minimum-stability": "dev",
29+
"prefer-stable": true,
30+
"config": {
31+
"sort-packages": true
32+
}
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeRhapsodie\GetUrlContentsEzTwigBundle;
6+
7+
use Symfony\Component\HttpKernel\Bundle\Bundle;
8+
9+
class CodeRhapsodieGetUrlContentsEzTwigBundle extends Bundle
10+
{
11+
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeRhapsodie\GetUrlContentsEzTwigBundle\DependencyInjection;
6+
7+
use Symfony\Component\Config\FileLocator;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11+
12+
class CodeRhapsodieGetUrlContentsEzTwigExtension extends Extension
13+
{
14+
public function load(array $configs, ContainerBuilder $container)
15+
{
16+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
17+
$loader->load('services.yaml');
18+
}
19+
}

src/Resources/config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
CodeRhapsodie\GetUrlContentsEzTwigBundle\Twig\GetUrlContentsExtension:
3+
tags: ['twig.extension']

src/Twig/GetUrlContentsExtension.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeRhapsodie\GetUrlContentsEzTwigBundle\Twig;
6+
7+
use Twig\Extension\AbstractExtension;
8+
use Twig\TwigFunction;
9+
10+
class GetUrlContentsExtension extends AbstractExtension
11+
{
12+
public function getFunctions()
13+
{
14+
return [
15+
new TwigFunction('get_url_contents', [$this, 'getUrlContents']),
16+
];
17+
}
18+
19+
public function getUrlContents(string $url)
20+
{
21+
$ch = curl_init($url);
22+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
23+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
24+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
25+
26+
$content = curl_exec($ch);
27+
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
28+
29+
if ($content === false || $code !== 200) {
30+
return '';
31+
}
32+
33+
return $content;
34+
}
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$eZTemplateOperatorArray = [
4+
[
5+
'class' => 'GetUrlContentsOperator',
6+
'operator_names' => ['get_url_contents'],
7+
],
8+
];
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
class GetUrlContentsOperator
4+
{
5+
public function operatorList()
6+
{
7+
return ['get_url_contents'];
8+
}
9+
10+
public function namedParameterPerOperator()
11+
{
12+
return true;
13+
}
14+
15+
public function namedParameterList()
16+
{
17+
return [
18+
'get_url_contents' => [
19+
'url' => [
20+
'type' => 'string',
21+
'required' => true,
22+
],
23+
],
24+
];
25+
}
26+
27+
public function modify(&$tpl, &$operatorName, &$operatorParameters, &$rootNamespace, &$currentNamespace, &$operatorValue, &$namedParameters)
28+
{
29+
$ch = curl_init($namedParameters['url']);
30+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
31+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
32+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
33+
34+
$content = curl_exec($ch);
35+
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
36+
37+
if ($content === false || $code !== 200) {
38+
$operatorValue = '';
39+
40+
return;
41+
}
42+
43+
$operatorValue = $content;
44+
}
45+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<software>
3+
<metadata>
4+
<name>GetUrlContentsOperator</name>
5+
<version>1.0.0</version>
6+
<license>The MIT License</license>
7+
</metadata>
8+
</software>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php /*
2+
3+
[TemplateSettings]
4+
ExtensionAutoloadPath[]=get_url_contents_operator
5+
6+
*/

0 commit comments

Comments
 (0)