Skip to content
This repository was archived by the owner on Jul 25, 2023. It is now read-only.

Commit d7637cb

Browse files
author
Darren Belding
committed
Initial commit
0 parents  commit d7637cb

File tree

13 files changed

+302
-0
lines changed

13 files changed

+302
-0
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COMPOSE_PROJECT_NAME=m2_meanbee_svghelper

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/magento

LICENSE

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

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Magento 2 SVG Helper
2+
3+
Simple module that provides a block level helper for reading in an SVG files source from a theme in Magento 2.
4+
5+
## Installation
6+
7+
Install this extension via Composer:
8+
9+
```
10+
composer config repositories.meanbee_svghelper vcs https://github.com/meanbee/magento2-svghelper
11+
composer require meanbee/magento2-svghelper
12+
```
13+
14+
## Development
15+
16+
### Setting up a development environment
17+
18+
A Docker development environment is included with the project:
19+
20+
```
21+
mkdir magento
22+
docker-compose up -d db # Allow a few seconds for the db to initalise
23+
docker-compose run --rm cli bash /src/setup.sh
24+
docker-compose up -d
25+
```
26+
27+
## Usage
28+
29+
The SVGHelper is set as data on every block. This means that in your template you can call:
30+
31+
```<?php echo $block->getData('svgHelper')->getViewSvg('pathtofile.svg') ?>```
32+
33+
or
34+
35+
```<?php echo $block->getData('svgHelper')->getViewSvg('Magento_Module::pathtofile.svg') ?>```
36+
37+
This will output the raw contents of the svg file.

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "meanbee/magento2-svghelper",
3+
"description": "Magento 2 module that provides a block level helper for reading in an SVG files source from a theme",
4+
"type": "magento2-module",
5+
"version": "1.0.0",
6+
"license": [
7+
"Commercial"
8+
],
9+
"repositories": {
10+
"magento": {
11+
"type": "composer",
12+
"url": "https://repo.magento.com/"
13+
}
14+
},
15+
"require": {
16+
"magento/framework": "100.1.*",
17+
"psr/log": "~1.0"
18+
},
19+
"authors": [
20+
{
21+
"name": "Darren Belding",
22+
"email": "darren.belding@meanbee.com"
23+
}
24+
],
25+
"autoload": {
26+
"files": [
27+
"src/registration.php"
28+
],
29+
"psr-4": {
30+
"Meanbee\\SVGHelper\\": "/src"
31+
}
32+
}
33+
}

docker-compose.yml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
version: "2"
2+
services:
3+
varnish:
4+
image: meanbee/magento2-varnish:latest
5+
hostname: m2-meanbee-svghelper.docker
6+
ports:
7+
- 80
8+
environment:
9+
- VIRTUAL_HOST=m2-meanbee-svghelper.docker
10+
- VIRTUAL_PORT=80
11+
- HTTPS_METHOD=noredirect
12+
- CERT_NAME=default
13+
links:
14+
- web
15+
16+
web:
17+
image: meanbee/magento2-nginx:1.9
18+
hostname: web.m2-meanbee-svghelper.docker
19+
ports:
20+
- 80
21+
volumes_from:
22+
- appdata
23+
env_file:
24+
- ./magento.env
25+
links:
26+
- fpm
27+
28+
fpm:
29+
image: meanbee/magento2-php:7.0-fpm
30+
hostname: fpm.m2-meanbee-svghelper.docker
31+
ports:
32+
- 9000
33+
volumes_from:
34+
- appdata
35+
env_file:
36+
- ./magento.env
37+
environment:
38+
- ENABLE_SENDMAIL=true
39+
- PHP_ENABLE_XDEBUG
40+
links:
41+
- db
42+
43+
cron:
44+
image: meanbee/magento2-php:7.0-cli
45+
hostname: cron.m2-meanbee-svghelper.docker
46+
command: run-cron
47+
volumes_from:
48+
- appdata
49+
env_file:
50+
- ./magento.env
51+
environment:
52+
- ENABLE_SENDMAIL=true
53+
links:
54+
- db
55+
56+
cli:
57+
image: meanbee/magento2-php:7.0-cli
58+
volumes_from:
59+
- appdata
60+
volumes:
61+
- ~/.composer:/root/.composer
62+
env_file:
63+
- ./magento.env
64+
environment:
65+
- COMPOSER_HOME=/root/.composer
66+
- COMPOSER_ALLOW_SUPERUSER=1
67+
- M2SETUP_INSTALL_DB=true
68+
- M2SETUP_VERSION=2.1.*
69+
- M2SETUP_USE_SAMPLE_DATA=true
70+
- M2SETUP_DB_HOST=db
71+
- M2SETUP_DB_NAME=magento2
72+
- M2SETUP_DB_USER=magento2
73+
- M2SETUP_DB_PASSWORD=magento2
74+
- M2SETUP_BASE_URL=https://m2-meanbee-svghelper.docker/
75+
- M2SETUP_BACKEND_FRONTNAME=admin
76+
- M2SETUP_ADMIN_FIRSTNAME=Admin
77+
- M2SETUP_ADMIN_LASTNAME=User
78+
- M2SETUP_ADMIN_EMAIL=admin@example.com
79+
- M2SETUP_ADMIN_USER=admin
80+
- M2SETUP_ADMIN_PASSWORD=password123
81+
links:
82+
- db
83+
84+
db:
85+
image: mariadb:10
86+
ports:
87+
- 3306
88+
volumes_from:
89+
- dbdata
90+
env_file:
91+
- ./magento.env
92+
environment:
93+
- MYSQL_ROOT_PASSWORD=magento2
94+
- MYSQL_USER=magento2
95+
- MYSQL_PASSWORD=magento2
96+
- MYSQL_DATABASE=magento2
97+
- TERM=dumb
98+
99+
appdata:
100+
image: cweagans/bg-sync
101+
volumes:
102+
- /var/www/magento
103+
- .:/src
104+
environment:
105+
- SYNC_SOURCE=/var/www/magento
106+
- SYNC_DESTINATION=/src/magento
107+
- SYNC_VERBOSE=1
108+
- SYNC_MAX_INOTIFY_WATCHES=64000
109+
privileged: true
110+
restart: always
111+
112+
dbdata:
113+
image: tianon/true
114+
volumes:
115+
- /var/lib/mysql

magento.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MAGENTO_RUN_MODE=developer
2+
PHP_MEMORY_LIMIT=2G

setup.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
set -e # Exit on error
4+
5+
# Install Magento 2 if necessary
6+
magento-installer
7+
8+
cd $MAGENTO_ROOT
9+
10+
# Add the extension via Composer
11+
composer config repositories.meanbee_svghelper '{"type": "path", "url": "/src", "options": {"symlink": true}}'
12+
13+
composer require "meanbee/magento2-svghelper" "@dev"
14+
15+
# Workaround for Magento only allowing template paths within the install root
16+
ln -s /src $MAGENTO_ROOT/src
17+
18+
# Enable the extension and run migrations
19+
magento-command module:enable Meanbee_SVGHelper
20+
magento-command setup:upgrade
21+
magento-command setup:static-content:deploy
22+
magento-command cache:flush

src/Helper/Data.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Meanbee\SVGHelper\Helper;
4+
5+
use \Magento\Framework\App\Helper\AbstractHelper;
6+
7+
class Data extends AbstractHelper
8+
{
9+
protected $_assetSource;
10+
protected $_assetRepository;
11+
12+
public function __construct(
13+
\Magento\Framework\App\Helper\Context $context,
14+
\Magento\Framework\View\Asset\Source $assetSource,
15+
\Magento\Framework\View\Asset\Repository $assetRepository
16+
)
17+
{
18+
$this->_assetSource = $assetSource;
19+
$this->_assetRepository = $assetRepository;
20+
21+
parent::__construct($context);
22+
}
23+
24+
/**
25+
* @param $path
26+
*
27+
* @return bool|string
28+
*/
29+
public function getViewSvg($path)
30+
{
31+
$file = $this->_assetRepository->createAsset($path);
32+
return $this->_assetSource->getContent($file);
33+
}
34+
35+
}

src/Plugin/BlockPlugin.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Meanbee\SVGHelper\Plugin;
4+
5+
class BlockPlugin
6+
{
7+
protected $_svgHelper;
8+
9+
public function __construct(
10+
\Meanbee\SVGHelper\Helper\Data $svgHelper
11+
)
12+
{
13+
$this->_svgHelper = $svgHelper;
14+
}
15+
16+
public function afterCreateBlock($subject, $result)
17+
{
18+
return $result->setData('svgHelper', $this->_svgHelper);
19+
}
20+
}

0 commit comments

Comments
 (0)