Awight has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/203945

Change subject: Implement a Handlebars form renderer
......................................................................

Implement a Handlebars form renderer

This is very bare-bones.  You can either edit the default file directly,
    gateway_forms/handlebars/index.html.handlebars
or point the global variable $wgDonationInterfaceTemplate to your development
template.

Bug: T95949
Change-Id: I10d8af3911ac53e3048f0fca47f314eada2cd849
---
M DonationInterface.php
M astropay_gateway/astropay.adapter.php
M composer.json
M composer.lock
M gateway_forms/Form.php
A gateway_forms/Handlebars.php
A gateway_forms/handlebars/index.html.handlebars
7 files changed, 117 insertions(+), 4 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DonationInterface 
refs/changes/45/203945/1

diff --git a/DonationInterface.php b/DonationInterface.php
index 00a78ed..5f56ca6 100644
--- a/DonationInterface.php
+++ b/DonationInterface.php
@@ -118,6 +118,7 @@
 
 //load all possible form classes
 $wgAutoloadClasses['Gateway_Form'] = $donationinterface_dir . 
'gateway_forms/Form.php';
+$wgAutoloadClasses['Gateway_Form_Handlebars'] = $donationinterface_dir . 
'gateway_forms/Handlebars.php';
 $wgAutoloadClasses['Gateway_Form_RapidHtml'] = $donationinterface_dir . 
'gateway_forms/RapidHtml.php';
 $wgAutoloadClasses['CountryCodes'] = $donationinterface_dir . 
'gateway_forms/includes/CountryCodes.php';
 $wgAutoloadClasses['ProvinceAbbreviations'] = $donationinterface_dir . 
'gateway_forms/includes/ProvinceAbbreviations.php';
@@ -238,6 +239,11 @@
 $wgDonationInterfaceHtmlFormDir = dirname( __FILE__ ) . 
"/gateway_forms/rapidhtml/html";
 $wgDonationInterfaceTest = false;
 
+/**
+ * Default top-level template file.
+ */
+$wgDonationInterfaceTemplate = __DIR__ . 
'/gateway_forms/handlebars/index.html.handlebars';
+
 //all of the following variables make sense to override directly,
 //or change "DonationInterface" to the gateway's id to override just for that 
gateway.
 //for instance: To override $wgDonationInterfaceUseSyslog just for 
GlobalCollect, add
diff --git a/astropay_gateway/astropay.adapter.php 
b/astropay_gateway/astropay.adapter.php
index 9ccf1a4..9557675 100644
--- a/astropay_gateway/astropay.adapter.php
+++ b/astropay_gateway/astropay.adapter.php
@@ -25,6 +25,10 @@
        const IDENTIFIER = 'astropay';
        const GLOBAL_PREFIX = 'wgAstropayGateway';
 
+       public function getFormClass() {
+               return 'Gateway_Form_Handlebars';
+       }
+
        public function getCommunicationType() {
                return 'namevalue';
        }
diff --git a/composer.json b/composer.json
index b67d86c..56cd4ff 100644
--- a/composer.json
+++ b/composer.json
@@ -20,6 +20,7 @@
        },
        "require": {
                "psr/log": "1.0.0",
-               "monolog/monolog": "1.12.0"
+               "monolog/monolog": "1.12.0",
+               "zordius/lightncandy": "0.18"
        }
 }
diff --git a/composer.lock b/composer.lock
index a9fac59..7f29c0e 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
         "Read more about it at 
http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file";,
         "This file is @generated automatically"
     ],
-    "hash": "ee21c14716f26b1f74a27244319420c9",
+    "hash": "4a4155b409938e9f59f43bdcb6dd9ae7",
     "packages": [
         {
             "name": "monolog/monolog",
@@ -115,6 +115,53 @@
                 "psr-3"
             ],
             "time": "2012-12-21 11:40:51"
+        },
+        {
+            "name": "zordius/lightncandy",
+            "version": "v0.18",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/zordius/lightncandy.git";,
+                "reference": "24be6909c37391f4648ce1fdf19036b11bd56d05"
+            },
+            "dist": {
+                "type": "zip",
+                "url": 
"https://api.github.com/repos/zordius/lightncandy/zipball/24be6909c37391f4648ce1fdf19036b11bd56d05";,
+                "reference": "24be6909c37391f4648ce1fdf19036b11bd56d05",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "4.0.17"
+            },
+            "type": "library",
+            "autoload": {
+                "classmap": [
+                    "src/lightncandy.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/";,
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Zordius Chen",
+                    "email": "[email protected]"
+                }
+            ],
+            "description": "An extremely fast PHP implementation of handlebars 
( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ).",
+            "homepage": "https://github.com/zordius/lightncandy";,
+            "keywords": [
+                "handlebars",
+                "logicless",
+                "mustache",
+                "php",
+                "template"
+            ],
+            "time": "2015-01-01 04:37:19"
         }
     ],
     "packages-dev": [],
diff --git a/gateway_forms/Form.php b/gateway_forms/Form.php
index 7204bf4..297d291 100644
--- a/gateway_forms/Form.php
+++ b/gateway_forms/Form.php
@@ -14,6 +14,11 @@
        protected $logger;
 
        /**
+        * @var GatewayAdapter
+        */
+       protected $gateway;
+
+       /**
         * Required method for returning the full HTML for a form.
         *
         * Code invoking forms will expect this method to be set.  Requiring 
only
@@ -24,9 +29,9 @@
         */
        abstract function getForm();
 
-       public function __construct( &$gateway ) {
+       public function __construct( $gateway ) {
 
-               $this->gateway = & $gateway;
+               $this->gateway = $gateway;
                $this->logger = DonationLoggerFactory::getLogger( $gateway );
                $gateway_errors = $this->gateway->getAllErrors();
 
diff --git a/gateway_forms/Handlebars.php b/gateway_forms/Handlebars.php
new file mode 100644
index 0000000..5e3497e
--- /dev/null
+++ b/gateway_forms/Handlebars.php
@@ -0,0 +1,47 @@
+<?php
+
+class Gateway_Form_Handlebars extends Gateway_Form {
+       protected $topLevelForm;
+
+       public function __construct( GatewayAdapter $gateway ) {
+               parent::__construct( $gateway );
+
+               // TODO: Don't hardcode like this.
+               global $wgDonationInterfaceTemplate;
+               $this->topLevelForm = $wgDonationInterfaceTemplate;
+       }
+
+       /**
+        * Return the HTML form with data added
+        */
+       public function getForm() {
+               $data = $this->gateway->getData_Unstaged_Escaped();
+
+               $template = file_get_contents( $this->topLevelForm );
+               if ( !$template ) {
+                       throw new Exception( "Template file unavailable: 
[{$this->topLevelForm}]" );
+               }
+
+               // TODO: Use MW-core implementation, once we're on REL1_25.
+               $code = LightnCandy::compile(
+                       $template,
+                       array(
+                               'flags' => LightnCandy::FLAG_ERROR_EXCEPTION,
+                       )
+               );
+               if ( !$code ) {
+                       throw new Exception( 'Couldn\'t compile template!' );
+               }
+               if ( substr( $code, 0, 5 ) === '<?php' ) {
+                       $code = substr( $code, 5 );
+               }
+               $renderer = eval( $code );
+               if ( !is_callable( $renderer ) ) {
+                       throw new Exception( 'Can\'t run compiled template!' );
+               }
+
+               $html = call_user_func( $renderer, $data, array() );
+
+               return $html;
+       }
+}
diff --git a/gateway_forms/handlebars/index.html.handlebars 
b/gateway_forms/handlebars/index.html.handlebars
new file mode 100644
index 0000000..4aeb47f
--- /dev/null
+++ b/gateway_forms/handlebars/index.html.handlebars
@@ -0,0 +1,3 @@
+You said you wanted to donate in "{{ currency_code }}",
+
+FOO

-- 
To view, visit https://gerrit.wikimedia.org/r/203945
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I10d8af3911ac53e3048f0fca47f314eada2cd849
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to