From d7637cb5601310f7fcf9aad8973d89b32bced517 Mon Sep 17 00:00:00 2001 From: Darren Belding Date: Tue, 31 Jan 2017 09:45:40 +0000 Subject: [PATCH] Initial commit --- .env | 1 + .gitignore | 1 + LICENSE | 20 +++++++ README.md | 37 ++++++++++++ composer.json | 33 +++++++++++ docker-compose.yml | 115 +++++++++++++++++++++++++++++++++++++ magento.env | 2 + setup.sh | 22 +++++++ src/Helper/Data.php | 35 +++++++++++ src/Plugin/BlockPlugin.php | 20 +++++++ src/etc/frontend/di.xml | 5 ++ src/etc/module.xml | 4 ++ src/registration.php | 7 +++ 13 files changed, 302 insertions(+) create mode 100644 .env create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 docker-compose.yml create mode 100644 magento.env create mode 100644 setup.sh create mode 100644 src/Helper/Data.php create mode 100644 src/Plugin/BlockPlugin.php create mode 100644 src/etc/frontend/di.xml create mode 100644 src/etc/module.xml create mode 100644 src/registration.php diff --git a/.env b/.env new file mode 100644 index 0000000..90894fc --- /dev/null +++ b/.env @@ -0,0 +1 @@ +COMPOSE_PROJECT_NAME=m2_meanbee_svghelper \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b24cec4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/magento \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e09ca15 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2017 Meanbee + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f953e84 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Magento 2 SVG Helper + +Simple module that provides a block level helper for reading in an SVG files source from a theme in Magento 2. + +## Installation + +Install this extension via Composer: + +``` +composer config repositories.meanbee_svghelper vcs https://github.com/meanbee/magento2-svghelper +composer require meanbee/magento2-svghelper +``` + +## Development + +### Setting up a development environment + +A Docker development environment is included with the project: + +``` +mkdir magento +docker-compose up -d db # Allow a few seconds for the db to initalise +docker-compose run --rm cli bash /src/setup.sh +docker-compose up -d +``` + +## Usage + +The SVGHelper is set as data on every block. This means that in your template you can call: + +```getData('svgHelper')->getViewSvg('pathtofile.svg') ?>``` + +or + +```getData('svgHelper')->getViewSvg('Magento_Module::pathtofile.svg') ?>``` + +This will output the raw contents of the svg file. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9e9d1e1 --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "meanbee/magento2-svghelper", + "description": "Magento 2 module that provides a block level helper for reading in an SVG files source from a theme", + "type": "magento2-module", + "version": "1.0.0", + "license": [ + "Commercial" + ], + "repositories": { + "magento": { + "type": "composer", + "url": "https://repo.magento.com/" + } + }, + "require": { + "magento/framework": "100.1.*", + "psr/log": "~1.0" + }, + "authors": [ + { + "name": "Darren Belding", + "email": "darren.belding@meanbee.com" + } + ], + "autoload": { + "files": [ + "src/registration.php" + ], + "psr-4": { + "Meanbee\\SVGHelper\\": "/src" + } + } +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..8c80675 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,115 @@ +version: "2" +services: + varnish: + image: meanbee/magento2-varnish:latest + hostname: m2-meanbee-svghelper.docker + ports: + - 80 + environment: + - VIRTUAL_HOST=m2-meanbee-svghelper.docker + - VIRTUAL_PORT=80 + - HTTPS_METHOD=noredirect + - CERT_NAME=default + links: + - web + + web: + image: meanbee/magento2-nginx:1.9 + hostname: web.m2-meanbee-svghelper.docker + ports: + - 80 + volumes_from: + - appdata + env_file: + - ./magento.env + links: + - fpm + + fpm: + image: meanbee/magento2-php:7.0-fpm + hostname: fpm.m2-meanbee-svghelper.docker + ports: + - 9000 + volumes_from: + - appdata + env_file: + - ./magento.env + environment: + - ENABLE_SENDMAIL=true + - PHP_ENABLE_XDEBUG + links: + - db + + cron: + image: meanbee/magento2-php:7.0-cli + hostname: cron.m2-meanbee-svghelper.docker + command: run-cron + volumes_from: + - appdata + env_file: + - ./magento.env + environment: + - ENABLE_SENDMAIL=true + links: + - db + + cli: + image: meanbee/magento2-php:7.0-cli + volumes_from: + - appdata + volumes: + - ~/.composer:/root/.composer + env_file: + - ./magento.env + environment: + - COMPOSER_HOME=/root/.composer + - COMPOSER_ALLOW_SUPERUSER=1 + - M2SETUP_INSTALL_DB=true + - M2SETUP_VERSION=2.1.* + - M2SETUP_USE_SAMPLE_DATA=true + - M2SETUP_DB_HOST=db + - M2SETUP_DB_NAME=magento2 + - M2SETUP_DB_USER=magento2 + - M2SETUP_DB_PASSWORD=magento2 + - M2SETUP_BASE_URL=https://m2-meanbee-svghelper.docker/ + - M2SETUP_BACKEND_FRONTNAME=admin + - M2SETUP_ADMIN_FIRSTNAME=Admin + - M2SETUP_ADMIN_LASTNAME=User + - M2SETUP_ADMIN_EMAIL=admin@example.com + - M2SETUP_ADMIN_USER=admin + - M2SETUP_ADMIN_PASSWORD=password123 + links: + - db + + db: + image: mariadb:10 + ports: + - 3306 + volumes_from: + - dbdata + env_file: + - ./magento.env + environment: + - MYSQL_ROOT_PASSWORD=magento2 + - MYSQL_USER=magento2 + - MYSQL_PASSWORD=magento2 + - MYSQL_DATABASE=magento2 + - TERM=dumb + + appdata: + image: cweagans/bg-sync + volumes: + - /var/www/magento + - .:/src + environment: + - SYNC_SOURCE=/var/www/magento + - SYNC_DESTINATION=/src/magento + - SYNC_VERBOSE=1 + - SYNC_MAX_INOTIFY_WATCHES=64000 + privileged: true + restart: always + + dbdata: + image: tianon/true + volumes: + - /var/lib/mysql \ No newline at end of file diff --git a/magento.env b/magento.env new file mode 100644 index 0000000..f6021d9 --- /dev/null +++ b/magento.env @@ -0,0 +1,2 @@ +MAGENTO_RUN_MODE=developer +PHP_MEMORY_LIMIT=2G \ No newline at end of file diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..f3dfc2c --- /dev/null +++ b/setup.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +set -e # Exit on error + +# Install Magento 2 if necessary +magento-installer + +cd $MAGENTO_ROOT + +# Add the extension via Composer +composer config repositories.meanbee_svghelper '{"type": "path", "url": "/src", "options": {"symlink": true}}' + +composer require "meanbee/magento2-svghelper" "@dev" + +# Workaround for Magento only allowing template paths within the install root +ln -s /src $MAGENTO_ROOT/src + +# Enable the extension and run migrations +magento-command module:enable Meanbee_SVGHelper +magento-command setup:upgrade +magento-command setup:static-content:deploy +magento-command cache:flush \ No newline at end of file diff --git a/src/Helper/Data.php b/src/Helper/Data.php new file mode 100644 index 0000000..1eb8181 --- /dev/null +++ b/src/Helper/Data.php @@ -0,0 +1,35 @@ +_assetSource = $assetSource; + $this->_assetRepository = $assetRepository; + + parent::__construct($context); + } + + /** + * @param $path + * + * @return bool|string + */ + public function getViewSvg($path) + { + $file = $this->_assetRepository->createAsset($path); + return $this->_assetSource->getContent($file); + } + +} diff --git a/src/Plugin/BlockPlugin.php b/src/Plugin/BlockPlugin.php new file mode 100644 index 0000000..6a18f34 --- /dev/null +++ b/src/Plugin/BlockPlugin.php @@ -0,0 +1,20 @@ +_svgHelper = $svgHelper; + } + + public function afterCreateBlock($subject, $result) + { + return $result->setData('svgHelper', $this->_svgHelper); + } +} diff --git a/src/etc/frontend/di.xml b/src/etc/frontend/di.xml new file mode 100644 index 0000000..57661a1 --- /dev/null +++ b/src/etc/frontend/di.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/src/etc/module.xml b/src/etc/module.xml new file mode 100644 index 0000000..2b4cf91 --- /dev/null +++ b/src/etc/module.xml @@ -0,0 +1,4 @@ + + + + diff --git a/src/registration.php b/src/registration.php new file mode 100644 index 0000000..7a58064 --- /dev/null +++ b/src/registration.php @@ -0,0 +1,7 @@ +