Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated to beta4

- Created service factories
- Updated configuration to address beta4 service configuration
  • Loading branch information...
commit 810cc284ec6740f2529b11f3f9c5906efa18b797 1 parent 908f4b3
Matthew Weier O'Phinney weierophinney authored
40 Module.php
View
@@ -2,26 +2,8 @@
namespace PhlyContact;
-use Zend\Form\View\HelperLoader as FormHelperLoader;
-use Zend\Module\Consumer\AutoloaderProvider;
-
-class Module implements AutoloaderProvider
+class Module
{
- public function init($manager)
- {
- $events = $manager->events();
- if (method_exists($events, 'getStaticConnections')) {
- $shared = $events->getStaticConnections();
- } elseif (method_exists($events, 'getSharedCollections')) {
- $shared = $events->getSharedCollections();
- } elseif (method_exists($events, 'getSharedManager')) {
- $shared = $events->getSharedManager();
- } else {
- return;
- }
-
- $shared->attach('bootstrap', 'bootstrap', array($this, 'onBootstrap'));
- }
public function getAutoloaderConfig()
{
return array(
@@ -40,24 +22,4 @@ public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
-
- public function onBootstrap($e)
- {
- $app = $e->getParam('application');
- $locator = $app->getLocator();
- $this->helperLoader = $locator->get('Zend\View\HelperLoader');
-
- $app->events()->attach('route', array($this, 'onRouteFinish'), -100);
- }
-
- public function onRouteFinish($e)
- {
- $matches = $e->getRouteMatch();
- $controller = $matches->getParam('controller');
- $namespace = substr($controller, 0, strpos($controller, '\\'));
- if ($namespace !== __NAMESPACE__) {
- return;
- }
- $this->helperLoader->registerPlugins(new FormHelperLoader());
- }
}
13 README.md
View
@@ -7,14 +7,17 @@ Requirements
----
* PHP >= 5.3.3
-* Zend Framework 2, specifically:
+* Zend Framework 2, beta4 or later, specifically:
* Zend\Captcha (used for CAPTCHA functionality on the forms)
- * Zend\Form (the contact form itself; currently supports beta3 or earlier)
+ * Zend\InputFilter (used for validating the contact form); in turn, uses:
+ * Zend\Filter
+ * Zend\Uri
+ * Zend\Validator
+ * Zend\Form (the contact form itself; currently supports beta4 or later)
* Zend\Mail (for sending the contact emails)
- * Zend\Module (implements a ZF2 module)
+ * Zend\ModuleManager (implements a ZF2 module)
* Zend\Mvc (provides a controller)
- * Zend\Validator (uses the Hostname validator in the contact form when
- validating email addresses)
+ * Zend\ServiceManager (provides service factories)
* Zend\View (provides view scripts for the PhpRenderer, and utilizes
ViewModels in the controller)
14 autoload_classmap.php
View
@@ -1,6 +1,12 @@
<?php
-return array (
- 'PhlyContact\\Controller\\ContactController' => __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'PhlyContact' . DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR . 'ContactController.php',
- 'PhlyContact\\Form\\ContactForm' => __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'PhlyContact' . DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR . 'ContactForm.php',
- 'PhlyContact\\Form\\ContactFilter' => __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'PhlyContact' . DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR . 'ContactFilter.php',
+// Generated by ZF2's ./bin/classmap_generator.php
+return array(
+ 'PhlyContact\Service\ContactControllerFactory' => __DIR__ . '/src/PhlyContact/Service/ContactControllerFactory.php',
+ 'PhlyContact\Service\ContactCaptchaFactory' => __DIR__ . '/src/PhlyContact/Service/ContactCaptchaFactory.php',
+ 'PhlyContact\Service\ContactFormFactory' => __DIR__ . '/src/PhlyContact/Service/ContactFormFactory.php',
+ 'PhlyContact\Service\ContactMailTransportFactory' => __DIR__ . '/src/PhlyContact/Service/ContactMailTransportFactory.php',
+ 'PhlyContact\Service\ContactMailMessageFactory' => __DIR__ . '/src/PhlyContact/Service/ContactMailMessageFactory.php',
+ 'PhlyContact\Controller\ContactController' => __DIR__ . '/src/PhlyContact/Controller/ContactController.php',
+ 'PhlyContact\Form\ContactFilter' => __DIR__ . '/src/PhlyContact/Form/ContactFilter.php',
+ 'PhlyContact\Form\ContactForm' => __DIR__ . '/src/PhlyContact/Form/ContactForm.php',
);
245 config/module.config.php
View
@@ -1,194 +1,87 @@
<?php
-$config = array('di' => array(
- 'definition' => array('class' => array(
- 'PhlyContact\Controller\ContactController' => array(
- 'setContactForm' => array(
- 'required' => true,
- 'form' => array(
- 'required' => true,
- 'type' => 'PhlyContact\Form\ContactForm',
- ),
- ),
- 'setMessage' => array(
- 'required' => true,
- 'message' => array(
- 'required' => true,
- 'type' => 'Zend\Mail\Message',
- ),
- ),
- 'setMailTransport' => array(
- 'required' => true,
- 'transport' => array(
- 'required' => true,
- 'type' => 'Zend\Mail\Transport\TransportInterface',
- ),
- ),
+return array(
+ 'phly_contact' => array(
+ 'captcha' => array(
+ 'class' => 'dumb',
),
- 'PhlyContact\Form\ContactForm' => array(
- '__construct' => array(
- 'required' => true,
- 'name' => array(
- 'required' => false,
- 'type' => false,
- ),
- 'captchaAdapter' => array(
- 'required' => true,
- 'type' => 'Zend\Captcha\AdapterInterface',
- ),
- ),
- 'setInputFilter' => array(
- 'required' => true,
- 'inputFilter' => array(
- 'required' => true,
- 'type' => 'PhlyContact\Form\ContactFilter',
- ),
- ),
+ 'form' => array(
+ 'name' => 'contact',
),
- // The following is provided in order to simplify configuration of a
- // ReCaptcha adapter.
- 'Zend\Captcha\ReCaptcha' => array(
- 'setPrivkey' => array(
- 'privkey' => array(
- 'required' => true,
- 'type' => false,
- ),
- ),
- 'setPubkey' => array(
- 'pubkey' => array(
- 'required' => true,
- 'type' => false,
- ),
- ),
- 'setOption' => array(
- 'key' => array(
- 'required' => false,
- 'type' => false,
- ),
- 'value' => array(
- 'required' => false,
- 'type' => false,
- ),
- ),
+ 'mail_transport' => array(
+ 'class' => 'Zend\Mail\Transport\Sendmail',
+ 'options' => array(
+ )
),
- // The following is provided in order to allow injection of a To:
- // address, From: address, and sender.
- 'Zend\Mail\Message' => array(
- 'addTo' => array(
- 'emailOrAddressList' => array(
- 'type' => false,
- 'required' => true,
- ),
- 'name' => array(
- 'type' => false,
- 'required' => false,
- ),
+ 'message' => array(
+ 'to' => array(
+ 'EMAIL HERE' => 'NAME HERE',
),
- 'addFrom' => array(
- 'emailOrAddressList' => array(
- 'type' => false,
- 'required' => true,
- ),
- 'name' => array(
- 'type' => false,
- 'required' => false,
- ),
+ 'sender' => array(
+ 'address' => 'EMAIL HERE',
+ 'name' => 'NAME HERE',
),
- 'setSender' => array(
- 'emailOrAddressList' => array(
- 'type' => false,
- 'required' => true,
- ),
- 'name' => array(
- 'type' => false,
- 'required' => false,
- ),
+ /*
+ 'from' => array(
+ 'EMAIL HERE' => 'NAME HERE',
),
+ */
),
- )),
- 'instance' => array(
- 'preferences' => array(
- 'Zend\Mail\Transport\TransportInterface' => 'Zend\Mail\Transport\Sendmail',
- 'Zend\Captcha\AdapterInterface' => 'Zend\Captcha\Dumb',
+ ),
+ 'controller' => array(
+ 'factories' => array(
+ 'PhlyContact\Controller\ContactController' => 'PhlyContact\Service\ContactControllerFactory',
),
-
- // Defaults for mail message... these will clue the end-user in that
- // they need to override in app configuration.
- 'Zend\Mail\Message' => array('parameters' => array(
- 'Zend\Mail\Message::addTo:emailOrAddressList' => 'EMAIL HERE',
- 'Zend\Mail\Message::addTo:name' => "NAME HERE",
- 'Zend\Mail\Message::setSender:emailOrAddressList' => 'EMAIL HERE',
- 'Zend\Mail\Message::setSender:name' => "NAME HERE",
- )),
-
- // Template map for the two templates we provide for the PhpRenderer
- 'Zend\View\Resolver\TemplateMapResolver' => array('parameters' => array(
- 'map' => array(
- 'contact/index' => __DIR__ . '/../view/phly-contact/contact/index.phtml',
- 'contact/thank-you' => __DIR__ . '/../view/phly-contact/contact/thank-you.phtml',
- ),
- )),
-
- // Paths for the TemplatePathStack if used
- 'Zend\View\Resolver\TemplatePathStack' => array('parameters' => array(
- 'paths' => array(
- 'contact' => __DIR__ . '/../view/phly-contact',
- ),
- )),
-
- // Injection points for the controller
- // Use the Sendmail mail transport by default
- 'PhlyContact\Controller\ContactController' => array('parameters' => array(
- 'message' => 'Zend\Mail\Message',
- 'form' => 'PhlyContact\Form\ContactForm',
- )),
-
- 'PhlyContact\Form\ContactForm' => array('parameters' => array(
- 'name' => 'contact',
- 'inputFilter' => 'PhlyContact\Form\ContactFilter',
- )),
-
- // If using a ReCaptcha, define these
- 'Zend\Captcha\ReCaptcha' => array('parameters' => array(
- 'pubkey' => 'RECAPTCHA_PUBKEY_HERE',
- 'privkey' => 'RECAPTCHA_PRIVKEY_HERE',
- )),
-
- // Routes
- 'Zend\Mvc\Router\RouteStackInterface' => array('parameters' => array(
- 'routes' => array(
- 'contact' => array(
- 'type' => 'Literal',
- 'options' => array(
- 'route' => '/contact',
- 'defaults' => array(
- 'controller' => 'PhlyContact\Controller\ContactController',
- 'action' => 'index',
- ),
+ ),
+ 'router' => array(
+ 'routes' => array(
+ 'contact' => array(
+ 'type' => 'Literal',
+ 'options' => array(
+ 'route' => '/contact',
+ 'defaults' => array(
+ 'controller' => 'PhlyContact\Controller\ContactController',
+ 'action' => 'index',
),
- 'may_terminate' => true,
- 'child_routes' => array(
- 'process' => array(
- 'type' => 'Literal',
- 'options' => array(
- 'route' => '/process',
- 'defaults' => array(
- 'action' => 'process',
- ),
+ ),
+ 'may_terminate' => true,
+ 'child_routes' => array(
+ 'process' => array(
+ 'type' => 'Literal',
+ 'options' => array(
+ 'route' => '/process',
+ 'defaults' => array(
+ 'action' => 'process',
),
),
- 'thank-you' => array(
- 'type' => 'Literal',
- 'options' => array(
- 'route' => '/thank-you',
- 'defaults' => array(
- 'action' => 'thank-you',
- ),
+ ),
+ 'thank-you' => array(
+ 'type' => 'Literal',
+ 'options' => array(
+ 'route' => '/thank-you',
+ 'defaults' => array(
+ 'action' => 'thank-you',
),
),
),
),
),
- )),
+ ),
+ ),
+ 'service_manager' => array(
+ 'factories' => array(
+ 'PhlyContactCaptcha' => 'PhlyContact\Service\ContactCaptchaFactory',
+ 'PhlyContactForm' => 'PhlyContact\Service\ContactFormFactory',
+ 'PhlyContactMailMessage' => 'PhlyContact\Service\ContactMailMessageFactory',
+ 'PhlyContactMailTransport' => 'PhlyContact\Service\ContactMailTransportFactory',
+ ),
+ ),
+ 'view_manager' => array(
+ 'template_map' => array(
+ 'contact/index' => __DIR__ . '/../view/phly-contact/contact/index.phtml',
+ 'contact/thank-you' => __DIR__ . '/../view/phly-contact/contact/thank-you.phtml',
+ ),
+ 'template_path_stack' => array(
+ 'contact' => __DIR__ . '/../view/phly-contact',
+ ),
),
-));
-return $config;
+);
57 config/module.phly-contact.local.config.php
View
@@ -8,33 +8,40 @@
* ReCaptcha CAPTCHA adapter, and setting the to and sender addresses for the
* mail message.
*/
-return array('di' => array(
- 'instance' => array(
- 'preferences' => array(
- 'Zend\Mail\Transport' => 'Zend\Mail\Transport\Smtp',
- 'Zend\Captcha\Adapter' => 'Zend\Captcha\ReCaptcha',
+return array(
+ 'phly_contact' => array(
+ // This is simply configuration to pass to Zend\Captcha\Factory
+ 'captcha' => array(
+ 'class' => 'recaptcha',
+ 'pubkey' => 'RECAPTCHA_PUBKEY_HERE',
+ 'privkey' => 'RECAPTCHA_PRIVKEY_HERE',
),
- 'Zend\Mail\Message' => array('parameters' => array(
- 'Zend\Mail\Message::addTo:emailOrAddressList' => 'contact@your.tld',
- 'Zend\Mail\Message::setSender:emailOrAddressList' => 'contact@your.tld',
- )),
+ // This sets the default "to" and "sender" headers for your message
+ 'message' => array(
+ // These can be either a string, or an array of email => name pairs
+ 'to' => 'contact@your.tld',
+ 'sender' => 'contact@your.tld',
+ ),
- // This is how to configure using GMail as your SMTP server
- 'Zend\Mail\Transport\SmtpOptions' => array('parameters' => array(
- 'host' => 'smtp.gmail.com',
- 'port' => 587,
- 'connectionClass' => 'login',
- 'connectionConfig' => array(
- 'ssl' => 'tls',
- 'username' => 'contact@your.tld',
- 'password' => 'password',
+ // Transport consists of two keys:
+ // - "class", the mail tranport class to use, and
+ // - "options", any options to use to configure the
+ // tranpsort. Usually these will be passed to the
+ // transport-specific options class
+ // This example configures GMail as your SMTP server
+ 'mail_transport' => array(
+ 'class' => 'Zend\Mail\Transport\Smtp',
+ 'options' => array(
+ 'host' => 'smtp.gmail.com',
+ 'port' => 587,
+ 'connectionClass' => 'login',
+ 'connectionConfig' => array(
+ 'ssl' => 'tls',
+ 'username' => 'contact@your.tld',
+ 'password' => 'password',
+ ),
),
- )),
-
- 'Zend\Captcha\ReCaptcha' => array('parameters' => array(
- 'pubkey' => 'RECAPTCHA_PUBKEY_HERE',
- 'privkey' => 'RECAPTCHA_PRIVKEY_HERE',
- )),
+ ),
),
-));
+);
18 src/PhlyContact/Service/ContactCaptchaFactory.php
View
@@ -0,0 +1,18 @@
+<?php
+
+namespace PhlyContact\Service;
+
+use Zend\Captcha\Factory as CaptchaFactory;
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+class ContactCaptchaFactory implements FactoryInterface
+{
+ public function createService(ServiceLocatorInterface $services)
+ {
+ $config = $services->get('config');
+ $spec = $config->phly_contact->captcha;
+ $captcha = CaptchaFactory::factory($spec);
+ return $captcha;
+ }
+}
29 src/PhlyContact/Service/ContactControllerFactory.php
View
@@ -0,0 +1,29 @@
+<?php
+
+namespace PhlyContact\Service;
+
+use PhlyContact\Controller\ContactController;
+use Zend\Mail\Message;
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+class ContactControllerFactory implements FactoryInterface
+{
+ public function createService(ServiceLocatorInterface $services)
+ {
+ $config = $services->get('config');
+ $form = $services->get('PhlyContactForm');
+ $message = $services->get('PhlyContactMailMessage');
+ $transport = $services->get('PhlyContactMailTransport');
+ $events = $services->get('EventManager');
+
+ $controller = new ContactController();
+ $controller->setServiceManager($services);
+ $controller->setEventManager($events);
+ $controller->setMessage($message);
+ $controller->setMailTransport($transport);
+ $controller->setContactForm($form);
+
+ return $controller;
+ }
+}
21 src/PhlyContact/Service/ContactFormFactory.php
View
@@ -0,0 +1,21 @@
+<?php
+
+namespace PhlyContact\Service;
+
+use PhlyContact\Form\ContactFilter;
+use PhlyContact\Form\ContactForm;
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+class ContactFormFactory implements FactoryInterface
+{
+ public function createService(ServiceLocatorInterface $services)
+ {
+ $config = $services->get('config');
+ $name = $config->phly_contact->form->name;
+ $captcha = $services->get('PhlyContactCaptcha');
+ $filter = new ContactFilter();
+ $form = new ContactForm($name, $captcha);
+ return $form;
+ }
+}
34 src/PhlyContact/Service/ContactMailMessageFactory.php
View
@@ -0,0 +1,34 @@
+<?php
+
+namespace PhlyContact\Service;
+
+use Zend\Mail\Message;
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+class ContactMailMessageFactory implements FactoryInterface
+{
+ public function createService(ServiceLocatorInterface $services)
+ {
+ $config = $services->get('config');
+ $config = $config->phly_contact->message;
+
+ $message = new Message();
+
+ if (isset($config->to)) {
+ $message->addTo($config->to);
+ }
+
+ if (isset($config->from)) {
+ $message->addFrom($config->from);
+ }
+
+ if (isset($config->sender) && isset($config->sender->address)) {
+ $address = $config->sender->address;
+ $name = isset($config->sender->name) ? $config->sender->name : null;
+ $message->setSender($address, $name);
+ }
+
+ return $message;
+ }
+}
46 src/PhlyContact/Service/ContactMailTransportFactory.php
View
@@ -0,0 +1,46 @@
+<?php
+
+namespace PhlyContact\Service;
+
+use Zend\Mail\Transport;
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+class ContactMailTransportFactory implements FactoryInterface
+{
+ public function createService(ServiceLocatorInterface $services)
+ {
+ $config = $services->get('config');
+ $config = $config->phly_contact->mail_transport;
+ $class = $config->class;
+ $options = $config->options;
+
+ switch ($class) {
+ case 'Zend\Mail\Transport\Sendmail':
+ case 'Sendmail':
+ case 'sendmail';
+ $transport = new Transport\Sendmail();
+ break;
+ case 'Zend\Mail\Transport\Smtp';
+ case 'Smtp';
+ case 'smtp';
+ $options = new Transport\SmtpOptions($options);
+ $transport = new Transport\Smtp($options);
+ break;
+ case 'Zend\Mail\Transport\File';
+ case 'File';
+ case 'file';
+ $options = new Transport\FileOptions($options);
+ $transport = new Transport\File($options);
+ break;
+ default:
+ throw new \DomainException(sprintf(
+ 'Unknown mail transport type provided ("%s")',
+ $class
+ ));
+ }
+
+ return $transport;
+ }
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.