@@ -16,184 +16,184 @@ |
||
| 16 | 16 | * `ConfigInterface` to the `ChainMail` constructor. |
| 17 | 17 | */ |
| 18 | 18 | return [ |
| 19 | - /* |
|
| 19 | + /* |
|
| 20 | 20 | * Root location that contains the view files to be rendered. These are |
| 21 | 21 | * referenced by the individual sections & templates when choosing a |
| 22 | 22 | * specific view to be rendered. |
| 23 | 23 | */ |
| 24 | - 'view_root_locations' => [ |
|
| 25 | - 'default' => realpath( __DIR__ . '/../views' ), |
|
| 26 | - ], |
|
| 24 | + 'view_root_locations' => [ |
|
| 25 | + 'default' => realpath( __DIR__ . '/../views' ), |
|
| 26 | + ], |
|
| 27 | 27 | |
| 28 | - /* |
|
| 28 | + /* |
|
| 29 | 29 | * The default template to when none is specified. |
| 30 | 30 | */ |
| 31 | - 'default_template' => 'BasicTemplate', |
|
| 31 | + 'default_template' => 'BasicTemplate', |
|
| 32 | 32 | |
| 33 | - /* |
|
| 33 | + /* |
|
| 34 | 34 | * The formats in which an email can be rendered. |
| 35 | 35 | * Each format needs a `MailInterface` implementation, a |
| 36 | 36 | * `ValidatorInterface` implementation, as well as a `SanitizerInterface` |
| 37 | 37 | * implementation. |
| 38 | 38 | */ |
| 39 | - 'formats' => [ |
|
| 39 | + 'formats' => [ |
|
| 40 | 40 | |
| 41 | - /* |
|
| 41 | + /* |
|
| 42 | 42 | * The `html` format should be the default format when using responsive |
| 43 | 43 | * emails. |
| 44 | 44 | */ |
| 45 | - 'html' => [ |
|
| 46 | - 'mail' => 'HTMLMail', |
|
| 47 | - 'validator' => 'HTMLValidator', |
|
| 48 | - 'sanitizer' => 'HTMLSanitizer', |
|
| 49 | - ], |
|
| 45 | + 'html' => [ |
|
| 46 | + 'mail' => 'HTMLMail', |
|
| 47 | + 'validator' => 'HTMLValidator', |
|
| 48 | + 'sanitizer' => 'HTMLSanitizer', |
|
| 49 | + ], |
|
| 50 | 50 | |
| 51 | - /* |
|
| 51 | + /* |
|
| 52 | 52 | * The `text` format is meant to be used a as a fallback, and can also |
| 53 | 53 | * be sent as backup content embedded within HTML emails. |
| 54 | 54 | */ |
| 55 | - 'text' => [ |
|
| 56 | - 'mail' => 'TextMail', |
|
| 57 | - 'validator' => 'TextValidator', |
|
| 58 | - 'sanitizer' => 'TextSanitizer', |
|
| 59 | - ], |
|
| 60 | - ], |
|
| 55 | + 'text' => [ |
|
| 56 | + 'mail' => 'TextMail', |
|
| 57 | + 'validator' => 'TextValidator', |
|
| 58 | + 'sanitizer' => 'TextSanitizer', |
|
| 59 | + ], |
|
| 60 | + ], |
|
| 61 | 61 | |
| 62 | - /* |
|
| 62 | + /* |
|
| 63 | 63 | * The `MailInterface` implementations that are provided. |
| 64 | 64 | */ |
| 65 | - 'mails' => [ |
|
| 66 | - 'HTMLMail' => [ |
|
| 67 | - 'class_name' => 'BrightNucleus\ChainMail\Mail\HTMLMail', |
|
| 68 | - ], |
|
| 69 | - 'TextMail' => [ |
|
| 70 | - 'class_name' => 'BrightNucleus\ChainMail\Mail\TextMail', |
|
| 71 | - ], |
|
| 72 | - ], |
|
| 65 | + 'mails' => [ |
|
| 66 | + 'HTMLMail' => [ |
|
| 67 | + 'class_name' => 'BrightNucleus\ChainMail\Mail\HTMLMail', |
|
| 68 | + ], |
|
| 69 | + 'TextMail' => [ |
|
| 70 | + 'class_name' => 'BrightNucleus\ChainMail\Mail\TextMail', |
|
| 71 | + ], |
|
| 72 | + ], |
|
| 73 | 73 | |
| 74 | - /* |
|
| 74 | + /* |
|
| 75 | 75 | * The `ValidatorInterface` implementations that are provided. |
| 76 | 76 | */ |
| 77 | - 'validators' => [ |
|
| 78 | - 'HTMLValidator' => [ |
|
| 79 | - 'class_name' => 'BrightNucleus\ChainMail\Validator\HTMLValidator', |
|
| 80 | - ], |
|
| 81 | - 'TextValidator' => [ |
|
| 82 | - 'class_name' => 'BrightNucleus\ChainMail\Validator\TextValidator', |
|
| 83 | - ], |
|
| 84 | - ], |
|
| 77 | + 'validators' => [ |
|
| 78 | + 'HTMLValidator' => [ |
|
| 79 | + 'class_name' => 'BrightNucleus\ChainMail\Validator\HTMLValidator', |
|
| 80 | + ], |
|
| 81 | + 'TextValidator' => [ |
|
| 82 | + 'class_name' => 'BrightNucleus\ChainMail\Validator\TextValidator', |
|
| 83 | + ], |
|
| 84 | + ], |
|
| 85 | 85 | |
| 86 | - /* |
|
| 86 | + /* |
|
| 87 | 87 | * The `SanitizerInterface` implementations that are provided. |
| 88 | 88 | */ |
| 89 | - 'sanitizers' => [ |
|
| 90 | - 'HTMLSanitizer' => [ |
|
| 91 | - 'class_name' => 'BrightNucleus\ChainMail\Sanitizer\HTMLSanitizer', |
|
| 92 | - ], |
|
| 93 | - 'TextSanitizer' => [ |
|
| 94 | - 'class_name' => 'BrightNucleus\ChainMail\Sanitizer\TextSanitizer', |
|
| 95 | - ], |
|
| 96 | - ], |
|
| 89 | + 'sanitizers' => [ |
|
| 90 | + 'HTMLSanitizer' => [ |
|
| 91 | + 'class_name' => 'BrightNucleus\ChainMail\Sanitizer\HTMLSanitizer', |
|
| 92 | + ], |
|
| 93 | + 'TextSanitizer' => [ |
|
| 94 | + 'class_name' => 'BrightNucleus\ChainMail\Sanitizer\TextSanitizer', |
|
| 95 | + ], |
|
| 96 | + ], |
|
| 97 | 97 | |
| 98 | - /* |
|
| 98 | + /* |
|
| 99 | 99 | * ViewBuilder configuration that is passed into the `brightnucleus/view` component. |
| 100 | 100 | */ |
| 101 | - 'ViewBuilder' => [ |
|
| 102 | - 'EngineFinder' => [ |
|
| 103 | - 'ClassName' => 'BrightNucleus\View\Engine\EngineFinder', |
|
| 104 | - 'Engines' => [ |
|
| 105 | - 'PHPEngine' => 'BrightNucleus\View\Engine\PHPEngine', |
|
| 106 | - ], |
|
| 107 | - 'NullObject' => 'BrightNucleus\View\Engine\NullEngine', |
|
| 108 | - ], |
|
| 109 | - 'ViewFinder' => [ |
|
| 110 | - 'ClassName' => 'BrightNucleus\View\View\ViewFinder', |
|
| 111 | - 'Views' => [ |
|
| 112 | - 'MailView' => 'BrightNucleus\ChainMail\View\MailView', |
|
| 113 | - ], |
|
| 114 | - 'NullObject' => 'BrightNucleus\View\View\NullView', |
|
| 115 | - ], |
|
| 116 | - ], |
|
| 101 | + 'ViewBuilder' => [ |
|
| 102 | + 'EngineFinder' => [ |
|
| 103 | + 'ClassName' => 'BrightNucleus\View\Engine\EngineFinder', |
|
| 104 | + 'Engines' => [ |
|
| 105 | + 'PHPEngine' => 'BrightNucleus\View\Engine\PHPEngine', |
|
| 106 | + ], |
|
| 107 | + 'NullObject' => 'BrightNucleus\View\Engine\NullEngine', |
|
| 108 | + ], |
|
| 109 | + 'ViewFinder' => [ |
|
| 110 | + 'ClassName' => 'BrightNucleus\View\View\ViewFinder', |
|
| 111 | + 'Views' => [ |
|
| 112 | + 'MailView' => 'BrightNucleus\ChainMail\View\MailView', |
|
| 113 | + ], |
|
| 114 | + 'NullObject' => 'BrightNucleus\View\View\NullView', |
|
| 115 | + ], |
|
| 116 | + ], |
|
| 117 | 117 | |
| 118 | - /* |
|
| 118 | + /* |
|
| 119 | 119 | * The `TemplateInterface` implementations that are provided. |
| 120 | 120 | * |
| 121 | 121 | * Each template also defines what sections it intends to use, and what |
| 122 | 122 | * view it intends to use for rendering. |
| 123 | 123 | */ |
| 124 | - 'templates' => [ |
|
| 125 | - 'BasicTemplate' => [ |
|
| 126 | - 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
| 127 | - 'sections' => [ |
|
| 128 | - 'HeaderSection', |
|
| 129 | - 'BodySection', |
|
| 130 | - 'FooterSection', |
|
| 131 | - ], |
|
| 132 | - 'view_name' => 'GenericTemplate', |
|
| 133 | - ], |
|
| 134 | - 'HeroTemplate' => [ |
|
| 135 | - 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
| 136 | - 'sections' => [ |
|
| 137 | - 'HeaderSection', |
|
| 138 | - 'HeroSection', |
|
| 139 | - 'BodySection', |
|
| 140 | - 'FooterSection', |
|
| 141 | - ], |
|
| 142 | - 'view_name' => 'GenericTemplate', |
|
| 143 | - ], |
|
| 144 | - 'SidebarTemplate' => [ |
|
| 145 | - 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
| 146 | - 'sections' => [ |
|
| 147 | - 'HeaderSection', |
|
| 148 | - 'BodySection', |
|
| 149 | - 'SidebarSection', |
|
| 150 | - 'FooterSection', |
|
| 151 | - ], |
|
| 152 | - 'view_name' => 'GenericTemplate', |
|
| 153 | - ], |
|
| 154 | - 'HeroSidebarTemplate' => [ |
|
| 155 | - 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
| 156 | - 'sections' => [ |
|
| 157 | - 'HeaderSection', |
|
| 158 | - 'HeroSection', |
|
| 159 | - 'BodySection', |
|
| 160 | - 'SidebarSection', |
|
| 161 | - 'FooterSection', |
|
| 162 | - ], |
|
| 163 | - 'view_name' => 'GenericTemplate', |
|
| 164 | - ], |
|
| 165 | - ], |
|
| 124 | + 'templates' => [ |
|
| 125 | + 'BasicTemplate' => [ |
|
| 126 | + 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
| 127 | + 'sections' => [ |
|
| 128 | + 'HeaderSection', |
|
| 129 | + 'BodySection', |
|
| 130 | + 'FooterSection', |
|
| 131 | + ], |
|
| 132 | + 'view_name' => 'GenericTemplate', |
|
| 133 | + ], |
|
| 134 | + 'HeroTemplate' => [ |
|
| 135 | + 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
| 136 | + 'sections' => [ |
|
| 137 | + 'HeaderSection', |
|
| 138 | + 'HeroSection', |
|
| 139 | + 'BodySection', |
|
| 140 | + 'FooterSection', |
|
| 141 | + ], |
|
| 142 | + 'view_name' => 'GenericTemplate', |
|
| 143 | + ], |
|
| 144 | + 'SidebarTemplate' => [ |
|
| 145 | + 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
| 146 | + 'sections' => [ |
|
| 147 | + 'HeaderSection', |
|
| 148 | + 'BodySection', |
|
| 149 | + 'SidebarSection', |
|
| 150 | + 'FooterSection', |
|
| 151 | + ], |
|
| 152 | + 'view_name' => 'GenericTemplate', |
|
| 153 | + ], |
|
| 154 | + 'HeroSidebarTemplate' => [ |
|
| 155 | + 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
| 156 | + 'sections' => [ |
|
| 157 | + 'HeaderSection', |
|
| 158 | + 'HeroSection', |
|
| 159 | + 'BodySection', |
|
| 160 | + 'SidebarSection', |
|
| 161 | + 'FooterSection', |
|
| 162 | + ], |
|
| 163 | + 'view_name' => 'GenericTemplate', |
|
| 164 | + ], |
|
| 165 | + ], |
|
| 166 | 166 | |
| 167 | - /* |
|
| 167 | + /* |
|
| 168 | 168 | * The `SectionInterface` implementations that are provided. |
| 169 | 169 | * |
| 170 | 170 | * Each section also defines what view it intends to use for rendering. |
| 171 | 171 | */ |
| 172 | - 'sections' => [ |
|
| 173 | - 'HeaderSection' => [ |
|
| 174 | - 'css_class' => 'header', |
|
| 175 | - 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
| 176 | - 'view_name' => 'GenericSection', |
|
| 177 | - ], |
|
| 178 | - 'BodySection' => [ |
|
| 179 | - 'css_class' => 'body', |
|
| 180 | - 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
| 181 | - 'view_name' => 'GenericSection', |
|
| 182 | - ], |
|
| 183 | - 'FooterSection' => [ |
|
| 184 | - 'css_class' => 'footer', |
|
| 185 | - 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
| 186 | - 'view_name' => 'GenericSection', |
|
| 187 | - ], |
|
| 188 | - 'HeroSection' => [ |
|
| 189 | - 'css_class' => 'hero', |
|
| 190 | - 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
| 191 | - 'view_name' => 'GenericSection', |
|
| 192 | - ], |
|
| 193 | - 'SidebarSection' => [ |
|
| 194 | - 'css_class' => 'sidebar', |
|
| 195 | - 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
| 196 | - 'view_name' => 'GenericSection', |
|
| 197 | - ], |
|
| 198 | - ], |
|
| 172 | + 'sections' => [ |
|
| 173 | + 'HeaderSection' => [ |
|
| 174 | + 'css_class' => 'header', |
|
| 175 | + 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
| 176 | + 'view_name' => 'GenericSection', |
|
| 177 | + ], |
|
| 178 | + 'BodySection' => [ |
|
| 179 | + 'css_class' => 'body', |
|
| 180 | + 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
| 181 | + 'view_name' => 'GenericSection', |
|
| 182 | + ], |
|
| 183 | + 'FooterSection' => [ |
|
| 184 | + 'css_class' => 'footer', |
|
| 185 | + 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
| 186 | + 'view_name' => 'GenericSection', |
|
| 187 | + ], |
|
| 188 | + 'HeroSection' => [ |
|
| 189 | + 'css_class' => 'hero', |
|
| 190 | + 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
| 191 | + 'view_name' => 'GenericSection', |
|
| 192 | + ], |
|
| 193 | + 'SidebarSection' => [ |
|
| 194 | + 'css_class' => 'sidebar', |
|
| 195 | + 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
| 196 | + 'view_name' => 'GenericSection', |
|
| 197 | + ], |
|
| 198 | + ], |
|
| 199 | 199 | ]; |
@@ -22,7 +22,7 @@ |
||
| 22 | 22 | * specific view to be rendered. |
| 23 | 23 | */ |
| 24 | 24 | 'view_root_locations' => [ |
| 25 | - 'default' => realpath( __DIR__ . '/../views' ), |
|
| 25 | + 'default' => realpath(__DIR__.'/../views'), |
|
| 26 | 26 | ], |
| 27 | 27 | |
| 28 | 28 | /* |
@@ -26,115 +26,115 @@ |
||
| 26 | 26 | class ChainMail |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - const DEFAULT_CONFIG = __DIR__ . '/../config/defaults.php'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Configuration Settings. |
|
| 33 | - * |
|
| 34 | - * @since 1.0.0 |
|
| 35 | - * |
|
| 36 | - * @var ConfigInterface |
|
| 37 | - */ |
|
| 38 | - protected $config; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Instantiate a ChainMail object. |
|
| 42 | - * |
|
| 43 | - * @since 1.0.0 |
|
| 44 | - * |
|
| 45 | - * @param ConfigInterface|null $config Optional. Configuration settings. |
|
| 46 | - */ |
|
| 47 | - public function __construct(ConfigInterface $config = null) |
|
| 48 | - { |
|
| 49 | - |
|
| 50 | - $defaults = ConfigFactory::create(include(self::DEFAULT_CONFIG)); |
|
| 51 | - |
|
| 52 | - if (! $config) { |
|
| 53 | - $this->config = $defaults; |
|
| 54 | - |
|
| 55 | - return; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - $this->config = ConfigFactory::create(array_merge( |
|
| 59 | - (array)$defaults, |
|
| 60 | - (array)$config) |
|
| 61 | - ); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Render a specific section. |
|
| 66 | - * |
|
| 67 | - * @since 1.0.0 |
|
| 68 | - * |
|
| 69 | - * @param string $sectionType Type of section to render. |
|
| 70 | - * @param array $context The context in which to render the section. |
|
| 71 | - * |
|
| 72 | - * @return string Rendered HTML. |
|
| 73 | - */ |
|
| 74 | - public static function renderSection($sectionType, array $context) |
|
| 75 | - { |
|
| 76 | - /** @var SectionInterface $section */ |
|
| 77 | - $section = $context['sections'][$sectionType]; |
|
| 78 | - |
|
| 79 | - return $section->render($context); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Get an array of strings representing the sections that are used by the |
|
| 84 | - * template. |
|
| 85 | - * |
|
| 86 | - * @since 1.0.0 |
|
| 87 | - * |
|
| 88 | - * @param array $context The context in which to render the section. |
|
| 89 | - * |
|
| 90 | - * @return array Array of strings with section types. |
|
| 91 | - */ |
|
| 92 | - public static function getUsedSections(array $context) |
|
| 93 | - { |
|
| 94 | - /** @var TemplateInterface $template */ |
|
| 95 | - $template = $context['template']; |
|
| 96 | - |
|
| 97 | - return $template->getUsedSections(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Render all used sections. |
|
| 102 | - * |
|
| 103 | - * @since 1.0.0 |
|
| 104 | - * |
|
| 105 | - * @param array $context The context in which to render the section. |
|
| 106 | - * |
|
| 107 | - * @return string Rendered HTML. |
|
| 108 | - */ |
|
| 109 | - public static function renderSections(array $context) |
|
| 110 | - { |
|
| 111 | - $output = ''; |
|
| 112 | - |
|
| 113 | - foreach (self::getUsedSections($context) as $section) { |
|
| 114 | - $output .= self::renderSection($section, $context); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - return $output; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Create a new mail object. |
|
| 122 | - * |
|
| 123 | - * @since 1.0.0 |
|
| 124 | - * |
|
| 125 | - * @param string $format Optional. Format to use. |
|
| 126 | - * @param string|TemplateInterface $template Optional. Template to be |
|
| 127 | - * used. |
|
| 128 | - * |
|
| 129 | - * @return MailInterface |
|
| 130 | - */ |
|
| 131 | - public function createMail($format = 'html', $template = 'BasicTemplate') |
|
| 132 | - { |
|
| 133 | - $mail_factory = new Factory($this->config, 'mails'); |
|
| 134 | - $mail_class = $this->config->getKey('formats')[$format]['mail']; |
|
| 135 | - $mail = $mail_factory->create($mail_class); |
|
| 136 | - $mail->setTemplate($template); |
|
| 137 | - |
|
| 138 | - return $mail; |
|
| 139 | - } |
|
| 29 | + const DEFAULT_CONFIG = __DIR__ . '/../config/defaults.php'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Configuration Settings. |
|
| 33 | + * |
|
| 34 | + * @since 1.0.0 |
|
| 35 | + * |
|
| 36 | + * @var ConfigInterface |
|
| 37 | + */ |
|
| 38 | + protected $config; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Instantiate a ChainMail object. |
|
| 42 | + * |
|
| 43 | + * @since 1.0.0 |
|
| 44 | + * |
|
| 45 | + * @param ConfigInterface|null $config Optional. Configuration settings. |
|
| 46 | + */ |
|
| 47 | + public function __construct(ConfigInterface $config = null) |
|
| 48 | + { |
|
| 49 | + |
|
| 50 | + $defaults = ConfigFactory::create(include(self::DEFAULT_CONFIG)); |
|
| 51 | + |
|
| 52 | + if (! $config) { |
|
| 53 | + $this->config = $defaults; |
|
| 54 | + |
|
| 55 | + return; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + $this->config = ConfigFactory::create(array_merge( |
|
| 59 | + (array)$defaults, |
|
| 60 | + (array)$config) |
|
| 61 | + ); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Render a specific section. |
|
| 66 | + * |
|
| 67 | + * @since 1.0.0 |
|
| 68 | + * |
|
| 69 | + * @param string $sectionType Type of section to render. |
|
| 70 | + * @param array $context The context in which to render the section. |
|
| 71 | + * |
|
| 72 | + * @return string Rendered HTML. |
|
| 73 | + */ |
|
| 74 | + public static function renderSection($sectionType, array $context) |
|
| 75 | + { |
|
| 76 | + /** @var SectionInterface $section */ |
|
| 77 | + $section = $context['sections'][$sectionType]; |
|
| 78 | + |
|
| 79 | + return $section->render($context); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Get an array of strings representing the sections that are used by the |
|
| 84 | + * template. |
|
| 85 | + * |
|
| 86 | + * @since 1.0.0 |
|
| 87 | + * |
|
| 88 | + * @param array $context The context in which to render the section. |
|
| 89 | + * |
|
| 90 | + * @return array Array of strings with section types. |
|
| 91 | + */ |
|
| 92 | + public static function getUsedSections(array $context) |
|
| 93 | + { |
|
| 94 | + /** @var TemplateInterface $template */ |
|
| 95 | + $template = $context['template']; |
|
| 96 | + |
|
| 97 | + return $template->getUsedSections(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Render all used sections. |
|
| 102 | + * |
|
| 103 | + * @since 1.0.0 |
|
| 104 | + * |
|
| 105 | + * @param array $context The context in which to render the section. |
|
| 106 | + * |
|
| 107 | + * @return string Rendered HTML. |
|
| 108 | + */ |
|
| 109 | + public static function renderSections(array $context) |
|
| 110 | + { |
|
| 111 | + $output = ''; |
|
| 112 | + |
|
| 113 | + foreach (self::getUsedSections($context) as $section) { |
|
| 114 | + $output .= self::renderSection($section, $context); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + return $output; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Create a new mail object. |
|
| 122 | + * |
|
| 123 | + * @since 1.0.0 |
|
| 124 | + * |
|
| 125 | + * @param string $format Optional. Format to use. |
|
| 126 | + * @param string|TemplateInterface $template Optional. Template to be |
|
| 127 | + * used. |
|
| 128 | + * |
|
| 129 | + * @return MailInterface |
|
| 130 | + */ |
|
| 131 | + public function createMail($format = 'html', $template = 'BasicTemplate') |
|
| 132 | + { |
|
| 133 | + $mail_factory = new Factory($this->config, 'mails'); |
|
| 134 | + $mail_class = $this->config->getKey('formats')[$format]['mail']; |
|
| 135 | + $mail = $mail_factory->create($mail_class); |
|
| 136 | + $mail->setTemplate($template); |
|
| 137 | + |
|
| 138 | + return $mail; |
|
| 139 | + } |
|
| 140 | 140 | } |
@@ -28,145 +28,145 @@ |
||
| 28 | 28 | abstract class AbstractSection implements SectionInterface |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Name of the Section. |
|
| 33 | - * |
|
| 34 | - * @since 1.0.0 |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $sectionName; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Configuration Settings. |
|
| 42 | - * |
|
| 43 | - * @since 1.0.0 |
|
| 44 | - * |
|
| 45 | - * @var ConfigInterface |
|
| 46 | - */ |
|
| 47 | - protected $config; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Content of the section. |
|
| 51 | - * |
|
| 52 | - * @since 1.0.0 |
|
| 53 | - * |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - protected $content; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * ViewBuilder to create template and section views. |
|
| 60 | - * |
|
| 61 | - * @since 1.0.0 |
|
| 62 | - * |
|
| 63 | - * @var ViewBuilder |
|
| 64 | - */ |
|
| 65 | - protected $viewBuilder; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Instantiate a AbstractSection object. |
|
| 69 | - * |
|
| 70 | - * @since 1.0.0 |
|
| 71 | - * |
|
| 72 | - * @param ConfigInterface $config Configuration settings. |
|
| 73 | - * @param array $arguments Arguments that are passed through the constructor. |
|
| 74 | - * Contained elements: |
|
| 75 | - * string $section, string $content |
|
| 76 | - * |
|
| 77 | - * @throws RuntimeException |
|
| 78 | - */ |
|
| 79 | - public function __construct($config, array $arguments) |
|
| 80 | - { |
|
| 81 | - $this->config = $config; |
|
| 82 | - list($section, $content, $this->viewBuilder) = $arguments; |
|
| 83 | - $this->setSectionName($section); |
|
| 84 | - $this->content = $content; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Get the name of the Section. |
|
| 89 | - * |
|
| 90 | - * @since 1.0.0 |
|
| 91 | - * |
|
| 92 | - * @return string Name of the section. |
|
| 93 | - */ |
|
| 94 | - public function getSectionName() |
|
| 95 | - { |
|
| 96 | - return $this->sectionName; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Set the name of the Section. |
|
| 101 | - * |
|
| 102 | - * @since 1.0.0 |
|
| 103 | - * |
|
| 104 | - * @param string|null $section Optional. Name of the section. |
|
| 105 | - * |
|
| 106 | - * @throws FailedToInitialiseSectionException If no section name was passed. |
|
| 107 | - * @throws FailedToInitialiseSectionException If an unknown section name was passed. |
|
| 108 | - */ |
|
| 109 | - protected function setSectionName($section = null) |
|
| 110 | - { |
|
| 111 | - if (null === $section) { |
|
| 112 | - throw new FailedToInitialiseSectionException( |
|
| 113 | - 'Initialised section without passing it a section name.' |
|
| 114 | - ); |
|
| 115 | - } |
|
| 116 | - if (! array_key_exists($section, $this->config['sections'])) { |
|
| 117 | - throw new FailedToInitialiseSectionException( |
|
| 118 | - 'Initialised section with an unknown section name.' |
|
| 119 | - ); |
|
| 120 | - } |
|
| 121 | - $this->sectionName = $section; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Render the current Renderable for a given context. |
|
| 126 | - * |
|
| 127 | - * @since 1.0.0 |
|
| 128 | - * |
|
| 129 | - * @param array $context The context in which to render the Renderable. |
|
| 130 | - * |
|
| 131 | - * @return string Rendered output of the Renderable. |
|
| 132 | - * @throws RuntimeException |
|
| 133 | - */ |
|
| 134 | - public function render(array $context) |
|
| 135 | - { |
|
| 136 | - |
|
| 137 | - $viewName = $this->getViewName($context); |
|
| 138 | - $view = $this->viewBuilder->create($viewName); |
|
| 139 | - |
|
| 140 | - $context['css_class'] = $this->getCSSClass(); |
|
| 141 | - $context['content'] = $this->content; |
|
| 142 | - |
|
| 143 | - return $view->render($context); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get the name of the View to use for rendering. |
|
| 148 | - * |
|
| 149 | - * @since 1.0.0 |
|
| 150 | - * |
|
| 151 | - * @param array $context The context in which to render the template. |
|
| 152 | - * |
|
| 153 | - * @return string Name of the view. |
|
| 154 | - */ |
|
| 155 | - protected function getViewName(array $context) |
|
| 156 | - { |
|
| 157 | - return $this->config['sections'][$this->getSectionName()]['view_name'] |
|
| 158 | - . '.' . $context['format']; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Get the CSS class that is used for the section. |
|
| 163 | - * |
|
| 164 | - * @since 1.0.0 |
|
| 165 | - * |
|
| 166 | - * @return string |
|
| 167 | - */ |
|
| 168 | - protected function getCSSClass() |
|
| 169 | - { |
|
| 170 | - return $this->config['sections'][$this->getSectionName()]['css_class']; |
|
| 171 | - } |
|
| 31 | + /** |
|
| 32 | + * Name of the Section. |
|
| 33 | + * |
|
| 34 | + * @since 1.0.0 |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $sectionName; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Configuration Settings. |
|
| 42 | + * |
|
| 43 | + * @since 1.0.0 |
|
| 44 | + * |
|
| 45 | + * @var ConfigInterface |
|
| 46 | + */ |
|
| 47 | + protected $config; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Content of the section. |
|
| 51 | + * |
|
| 52 | + * @since 1.0.0 |
|
| 53 | + * |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + protected $content; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * ViewBuilder to create template and section views. |
|
| 60 | + * |
|
| 61 | + * @since 1.0.0 |
|
| 62 | + * |
|
| 63 | + * @var ViewBuilder |
|
| 64 | + */ |
|
| 65 | + protected $viewBuilder; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Instantiate a AbstractSection object. |
|
| 69 | + * |
|
| 70 | + * @since 1.0.0 |
|
| 71 | + * |
|
| 72 | + * @param ConfigInterface $config Configuration settings. |
|
| 73 | + * @param array $arguments Arguments that are passed through the constructor. |
|
| 74 | + * Contained elements: |
|
| 75 | + * string $section, string $content |
|
| 76 | + * |
|
| 77 | + * @throws RuntimeException |
|
| 78 | + */ |
|
| 79 | + public function __construct($config, array $arguments) |
|
| 80 | + { |
|
| 81 | + $this->config = $config; |
|
| 82 | + list($section, $content, $this->viewBuilder) = $arguments; |
|
| 83 | + $this->setSectionName($section); |
|
| 84 | + $this->content = $content; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Get the name of the Section. |
|
| 89 | + * |
|
| 90 | + * @since 1.0.0 |
|
| 91 | + * |
|
| 92 | + * @return string Name of the section. |
|
| 93 | + */ |
|
| 94 | + public function getSectionName() |
|
| 95 | + { |
|
| 96 | + return $this->sectionName; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Set the name of the Section. |
|
| 101 | + * |
|
| 102 | + * @since 1.0.0 |
|
| 103 | + * |
|
| 104 | + * @param string|null $section Optional. Name of the section. |
|
| 105 | + * |
|
| 106 | + * @throws FailedToInitialiseSectionException If no section name was passed. |
|
| 107 | + * @throws FailedToInitialiseSectionException If an unknown section name was passed. |
|
| 108 | + */ |
|
| 109 | + protected function setSectionName($section = null) |
|
| 110 | + { |
|
| 111 | + if (null === $section) { |
|
| 112 | + throw new FailedToInitialiseSectionException( |
|
| 113 | + 'Initialised section without passing it a section name.' |
|
| 114 | + ); |
|
| 115 | + } |
|
| 116 | + if (! array_key_exists($section, $this->config['sections'])) { |
|
| 117 | + throw new FailedToInitialiseSectionException( |
|
| 118 | + 'Initialised section with an unknown section name.' |
|
| 119 | + ); |
|
| 120 | + } |
|
| 121 | + $this->sectionName = $section; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Render the current Renderable for a given context. |
|
| 126 | + * |
|
| 127 | + * @since 1.0.0 |
|
| 128 | + * |
|
| 129 | + * @param array $context The context in which to render the Renderable. |
|
| 130 | + * |
|
| 131 | + * @return string Rendered output of the Renderable. |
|
| 132 | + * @throws RuntimeException |
|
| 133 | + */ |
|
| 134 | + public function render(array $context) |
|
| 135 | + { |
|
| 136 | + |
|
| 137 | + $viewName = $this->getViewName($context); |
|
| 138 | + $view = $this->viewBuilder->create($viewName); |
|
| 139 | + |
|
| 140 | + $context['css_class'] = $this->getCSSClass(); |
|
| 141 | + $context['content'] = $this->content; |
|
| 142 | + |
|
| 143 | + return $view->render($context); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get the name of the View to use for rendering. |
|
| 148 | + * |
|
| 149 | + * @since 1.0.0 |
|
| 150 | + * |
|
| 151 | + * @param array $context The context in which to render the template. |
|
| 152 | + * |
|
| 153 | + * @return string Name of the view. |
|
| 154 | + */ |
|
| 155 | + protected function getViewName(array $context) |
|
| 156 | + { |
|
| 157 | + return $this->config['sections'][$this->getSectionName()]['view_name'] |
|
| 158 | + . '.' . $context['format']; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Get the CSS class that is used for the section. |
|
| 163 | + * |
|
| 164 | + * @since 1.0.0 |
|
| 165 | + * |
|
| 166 | + * @return string |
|
| 167 | + */ |
|
| 168 | + protected function getCSSClass() |
|
| 169 | + { |
|
| 170 | + return $this->config['sections'][$this->getSectionName()]['css_class']; |
|
| 171 | + } |
|
| 172 | 172 | } |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | 'Initialised section without passing it a section name.' |
| 114 | 114 | ); |
| 115 | 115 | } |
| 116 | - if (! array_key_exists($section, $this->config['sections'])) { |
|
| 116 | + if ( ! array_key_exists($section, $this->config['sections'])) { |
|
| 117 | 117 | throw new FailedToInitialiseSectionException( |
| 118 | 118 | 'Initialised section with an unknown section name.' |
| 119 | 119 | ); |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | protected function getViewName(array $context) |
| 156 | 156 | { |
| 157 | 157 | return $this->config['sections'][$this->getSectionName()]['view_name'] |
| 158 | - . '.' . $context['format']; |
|
| 158 | + . '.'.$context['format']; |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | /** |
@@ -24,17 +24,17 @@ |
||
| 24 | 24 | class MailView extends AbstractView |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Check whether the Findable can handle an individual criterion. |
|
| 29 | - * |
|
| 30 | - * @since 0.1.0 |
|
| 31 | - * |
|
| 32 | - * @param mixed $criterion Criterion to check. |
|
| 33 | - * |
|
| 34 | - * @return bool Whether the Findable can handle the criterion. |
|
| 35 | - */ |
|
| 36 | - public function canHandle($criterion) |
|
| 37 | - { |
|
| 38 | - return $criterion === $this->uri; |
|
| 39 | - } |
|
| 27 | + /** |
|
| 28 | + * Check whether the Findable can handle an individual criterion. |
|
| 29 | + * |
|
| 30 | + * @since 0.1.0 |
|
| 31 | + * |
|
| 32 | + * @param mixed $criterion Criterion to check. |
|
| 33 | + * |
|
| 34 | + * @return bool Whether the Findable can handle the criterion. |
|
| 35 | + */ |
|
| 36 | + public function canHandle($criterion) |
|
| 37 | + { |
|
| 38 | + return $criterion === $this->uri; |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -34,258 +34,258 @@ |
||
| 34 | 34 | abstract class AbstractMail implements MailInterface |
| 35 | 35 | { |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Configuration Settings. |
|
| 39 | - * |
|
| 40 | - * @since 1.0.0 |
|
| 41 | - * |
|
| 42 | - * @var ConfigInterface |
|
| 43 | - */ |
|
| 44 | - protected $config; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Template that is used for the email. |
|
| 48 | - * |
|
| 49 | - * @since 1.0.0 |
|
| 50 | - * |
|
| 51 | - * @var TemplateInterface |
|
| 52 | - */ |
|
| 53 | - protected $template; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Content for the different sections. |
|
| 57 | - * |
|
| 58 | - * @since 1.0.0 |
|
| 59 | - * |
|
| 60 | - * @var array |
|
| 61 | - */ |
|
| 62 | - protected $sectionContent = []; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Format of the mail. |
|
| 66 | - * |
|
| 67 | - * @since 1.0.0 |
|
| 68 | - * |
|
| 69 | - * @var string |
|
| 70 | - */ |
|
| 71 | - protected $format; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * ViewBuilder to create template and section views. |
|
| 75 | - * |
|
| 76 | - * @since 1.0.0 |
|
| 77 | - * |
|
| 78 | - * @var ViewBuilder |
|
| 79 | - */ |
|
| 80 | - protected $viewBuilder; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Instantiate an AbstractMail object. |
|
| 84 | - * |
|
| 85 | - * @since 1.0.0 |
|
| 86 | - * |
|
| 87 | - * @param ConfigInterface $config The Config to use. |
|
| 88 | - * |
|
| 89 | - * @throws FailedToProcessConfigException If the Config could not be processed. |
|
| 90 | - */ |
|
| 91 | - public function __construct(ConfigInterface $config) |
|
| 92 | - { |
|
| 93 | - $this->config = $config; |
|
| 94 | - $this->setFormat(); |
|
| 95 | - |
|
| 96 | - $this->viewBuilder = new ViewBuilder($config |
|
| 97 | - ? $config->getSubConfig('ViewBuilder') |
|
| 98 | - : View::getDefaultConfig() |
|
| 99 | - ); |
|
| 100 | - |
|
| 101 | - foreach ($this->config->getKey('view_root_locations') as $folder) { |
|
| 102 | - $this->viewBuilder->addLocation( |
|
| 103 | - new FilesystemLocation($folder) |
|
| 104 | - ); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get the template to use for the renderer. |
|
| 110 | - * |
|
| 111 | - * @since 1.0.0 |
|
| 112 | - * |
|
| 113 | - * @return TemplateInterface Reference to the template that is used. |
|
| 114 | - * @throws RuntimeException |
|
| 115 | - */ |
|
| 116 | - public function getTemplate() |
|
| 117 | - { |
|
| 118 | - |
|
| 119 | - if (! $this->template) { |
|
| 120 | - $this->setDefaultTemplate(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if (is_string($this->template)) { |
|
| 124 | - $this->template = $this->createTemplate($this->template); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - return $this->template; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Set the template to use for the renderer. |
|
| 132 | - * |
|
| 133 | - * @since 1.0.0 |
|
| 134 | - * |
|
| 135 | - * @param string|TemplateInterface $template Template to use for the |
|
| 136 | - * renderer. |
|
| 137 | - * |
|
| 138 | - * @throws InvalidTemplateException If the template class could not be instantiated. |
|
| 139 | - * @throws InvalidTemplateException If the template type is not recognized. |
|
| 140 | - */ |
|
| 141 | - public function setTemplate($template) |
|
| 142 | - { |
|
| 143 | - try { |
|
| 144 | - if (is_string($template)) { |
|
| 145 | - $template = $this->createTemplate($template); |
|
| 146 | - } |
|
| 147 | - } catch (Exception $exception) { |
|
| 148 | - throw new InvalidTemplateException( |
|
| 149 | - 'Could not instantiate the template class "%1$s". Reason: "%2$s".', |
|
| 150 | - $template, |
|
| 151 | - $exception->getMessage() |
|
| 152 | - ); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if (! $template instanceof TemplateInterface) { |
|
| 156 | - throw new InvalidTemplateException( |
|
| 157 | - 'Could not set the template, invalid type.', |
|
| 158 | - (array)$template |
|
| 159 | - ); |
|
| 160 | - } |
|
| 161 | - $this->template = $template; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Add a section to the Mail. |
|
| 166 | - * |
|
| 167 | - * @since 1.0.0 |
|
| 168 | - * |
|
| 169 | - * @param string $type Type of section to add. |
|
| 170 | - * @param string $content Content of the section. |
|
| 171 | - * |
|
| 172 | - * @throws RuntimeException |
|
| 173 | - */ |
|
| 174 | - public function addSection($type, $content) |
|
| 175 | - { |
|
| 176 | - $this->sectionContent[$type] = $content; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Render the Mail for a given context. |
|
| 181 | - * |
|
| 182 | - * @since 1.0.0 |
|
| 183 | - * |
|
| 184 | - * @param array $context The context in which to render the email. |
|
| 185 | - * |
|
| 186 | - * @return string Rendered output of the email |
|
| 187 | - */ |
|
| 188 | - public function render(array $context) |
|
| 189 | - { |
|
| 190 | - $template = $this->getTemplate(); |
|
| 191 | - |
|
| 192 | - $context['template'] = $template; |
|
| 193 | - |
|
| 194 | - $this->instantiateSections($template->getUsedSections(), $context); |
|
| 195 | - |
|
| 196 | - $context['format'] = $this->getFormat(); |
|
| 197 | - |
|
| 198 | - $context = $this->setContext($context); |
|
| 199 | - |
|
| 200 | - return $template->render($context); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Instantiate the requested sections for a template. |
|
| 205 | - * |
|
| 206 | - * @since 1.0.0 |
|
| 207 | - * |
|
| 208 | - * @param array $sections Sections to instantiate. |
|
| 209 | - * @param array $context The context in which to instantiate the sections. |
|
| 210 | - */ |
|
| 211 | - protected function instantiateSections(array $sections, array &$context) |
|
| 212 | - { |
|
| 213 | - $sectionFactory = new Factory($this->config, 'sections'); |
|
| 214 | - |
|
| 215 | - foreach ($sections as $section) { |
|
| 216 | - |
|
| 217 | - $content = null; |
|
| 218 | - |
|
| 219 | - if (array_key_exists($section, $this->sectionContent)) { |
|
| 220 | - $content = $this->sectionContent[$section]; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $context['sections'][$section] = $sectionFactory->create( |
|
| 224 | - $section, |
|
| 225 | - [$section, $content, $this->viewBuilder] |
|
| 226 | - ); |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Set the format of the mail. |
|
| 232 | - * |
|
| 233 | - * @since 1.0.0 |
|
| 234 | - * |
|
| 235 | - * @return string Format of the Mail. |
|
| 236 | - */ |
|
| 237 | - protected function getFormat() |
|
| 238 | - { |
|
| 239 | - return $this->format; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Set the format of the mail. |
|
| 244 | - * |
|
| 245 | - * @since 1.0.0 |
|
| 246 | - * |
|
| 247 | - * @return void |
|
| 248 | - */ |
|
| 249 | - abstract protected function setFormat(); |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Set the template to the default template defined in the configuration. |
|
| 253 | - * |
|
| 254 | - * @since 1.0.0 |
|
| 255 | - * |
|
| 256 | - * @throws RuntimeException |
|
| 257 | - */ |
|
| 258 | - protected function setDefaultTemplate() |
|
| 259 | - { |
|
| 260 | - $defaultTemplate = $this->config->getKey('default_template'); |
|
| 261 | - $this->setTemplate($defaultTemplate); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Create an instance of a template. |
|
| 266 | - * |
|
| 267 | - * @since 1.0.0 |
|
| 268 | - * |
|
| 269 | - * @param string $template Template to instantiate. |
|
| 270 | - * |
|
| 271 | - * @return TemplateInterface $template Newly created instance. |
|
| 272 | - * @throws RuntimeException |
|
| 273 | - */ |
|
| 274 | - protected function createTemplate($template) |
|
| 275 | - { |
|
| 276 | - $templateFactory = new Factory($this->config, 'templates'); |
|
| 277 | - |
|
| 278 | - return $templateFactory->create($template, [$template, $this->viewBuilder]); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Set the context of the mail. |
|
| 283 | - * |
|
| 284 | - * @since 1.0.0 |
|
| 285 | - * |
|
| 286 | - * @param array $context Context to set/modify. |
|
| 287 | - * |
|
| 288 | - * @return array Updated context. |
|
| 289 | - */ |
|
| 290 | - abstract protected function setContext(array $context); |
|
| 37 | + /** |
|
| 38 | + * Configuration Settings. |
|
| 39 | + * |
|
| 40 | + * @since 1.0.0 |
|
| 41 | + * |
|
| 42 | + * @var ConfigInterface |
|
| 43 | + */ |
|
| 44 | + protected $config; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Template that is used for the email. |
|
| 48 | + * |
|
| 49 | + * @since 1.0.0 |
|
| 50 | + * |
|
| 51 | + * @var TemplateInterface |
|
| 52 | + */ |
|
| 53 | + protected $template; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Content for the different sections. |
|
| 57 | + * |
|
| 58 | + * @since 1.0.0 |
|
| 59 | + * |
|
| 60 | + * @var array |
|
| 61 | + */ |
|
| 62 | + protected $sectionContent = []; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Format of the mail. |
|
| 66 | + * |
|
| 67 | + * @since 1.0.0 |
|
| 68 | + * |
|
| 69 | + * @var string |
|
| 70 | + */ |
|
| 71 | + protected $format; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * ViewBuilder to create template and section views. |
|
| 75 | + * |
|
| 76 | + * @since 1.0.0 |
|
| 77 | + * |
|
| 78 | + * @var ViewBuilder |
|
| 79 | + */ |
|
| 80 | + protected $viewBuilder; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Instantiate an AbstractMail object. |
|
| 84 | + * |
|
| 85 | + * @since 1.0.0 |
|
| 86 | + * |
|
| 87 | + * @param ConfigInterface $config The Config to use. |
|
| 88 | + * |
|
| 89 | + * @throws FailedToProcessConfigException If the Config could not be processed. |
|
| 90 | + */ |
|
| 91 | + public function __construct(ConfigInterface $config) |
|
| 92 | + { |
|
| 93 | + $this->config = $config; |
|
| 94 | + $this->setFormat(); |
|
| 95 | + |
|
| 96 | + $this->viewBuilder = new ViewBuilder($config |
|
| 97 | + ? $config->getSubConfig('ViewBuilder') |
|
| 98 | + : View::getDefaultConfig() |
|
| 99 | + ); |
|
| 100 | + |
|
| 101 | + foreach ($this->config->getKey('view_root_locations') as $folder) { |
|
| 102 | + $this->viewBuilder->addLocation( |
|
| 103 | + new FilesystemLocation($folder) |
|
| 104 | + ); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get the template to use for the renderer. |
|
| 110 | + * |
|
| 111 | + * @since 1.0.0 |
|
| 112 | + * |
|
| 113 | + * @return TemplateInterface Reference to the template that is used. |
|
| 114 | + * @throws RuntimeException |
|
| 115 | + */ |
|
| 116 | + public function getTemplate() |
|
| 117 | + { |
|
| 118 | + |
|
| 119 | + if (! $this->template) { |
|
| 120 | + $this->setDefaultTemplate(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if (is_string($this->template)) { |
|
| 124 | + $this->template = $this->createTemplate($this->template); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + return $this->template; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Set the template to use for the renderer. |
|
| 132 | + * |
|
| 133 | + * @since 1.0.0 |
|
| 134 | + * |
|
| 135 | + * @param string|TemplateInterface $template Template to use for the |
|
| 136 | + * renderer. |
|
| 137 | + * |
|
| 138 | + * @throws InvalidTemplateException If the template class could not be instantiated. |
|
| 139 | + * @throws InvalidTemplateException If the template type is not recognized. |
|
| 140 | + */ |
|
| 141 | + public function setTemplate($template) |
|
| 142 | + { |
|
| 143 | + try { |
|
| 144 | + if (is_string($template)) { |
|
| 145 | + $template = $this->createTemplate($template); |
|
| 146 | + } |
|
| 147 | + } catch (Exception $exception) { |
|
| 148 | + throw new InvalidTemplateException( |
|
| 149 | + 'Could not instantiate the template class "%1$s". Reason: "%2$s".', |
|
| 150 | + $template, |
|
| 151 | + $exception->getMessage() |
|
| 152 | + ); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if (! $template instanceof TemplateInterface) { |
|
| 156 | + throw new InvalidTemplateException( |
|
| 157 | + 'Could not set the template, invalid type.', |
|
| 158 | + (array)$template |
|
| 159 | + ); |
|
| 160 | + } |
|
| 161 | + $this->template = $template; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Add a section to the Mail. |
|
| 166 | + * |
|
| 167 | + * @since 1.0.0 |
|
| 168 | + * |
|
| 169 | + * @param string $type Type of section to add. |
|
| 170 | + * @param string $content Content of the section. |
|
| 171 | + * |
|
| 172 | + * @throws RuntimeException |
|
| 173 | + */ |
|
| 174 | + public function addSection($type, $content) |
|
| 175 | + { |
|
| 176 | + $this->sectionContent[$type] = $content; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Render the Mail for a given context. |
|
| 181 | + * |
|
| 182 | + * @since 1.0.0 |
|
| 183 | + * |
|
| 184 | + * @param array $context The context in which to render the email. |
|
| 185 | + * |
|
| 186 | + * @return string Rendered output of the email |
|
| 187 | + */ |
|
| 188 | + public function render(array $context) |
|
| 189 | + { |
|
| 190 | + $template = $this->getTemplate(); |
|
| 191 | + |
|
| 192 | + $context['template'] = $template; |
|
| 193 | + |
|
| 194 | + $this->instantiateSections($template->getUsedSections(), $context); |
|
| 195 | + |
|
| 196 | + $context['format'] = $this->getFormat(); |
|
| 197 | + |
|
| 198 | + $context = $this->setContext($context); |
|
| 199 | + |
|
| 200 | + return $template->render($context); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Instantiate the requested sections for a template. |
|
| 205 | + * |
|
| 206 | + * @since 1.0.0 |
|
| 207 | + * |
|
| 208 | + * @param array $sections Sections to instantiate. |
|
| 209 | + * @param array $context The context in which to instantiate the sections. |
|
| 210 | + */ |
|
| 211 | + protected function instantiateSections(array $sections, array &$context) |
|
| 212 | + { |
|
| 213 | + $sectionFactory = new Factory($this->config, 'sections'); |
|
| 214 | + |
|
| 215 | + foreach ($sections as $section) { |
|
| 216 | + |
|
| 217 | + $content = null; |
|
| 218 | + |
|
| 219 | + if (array_key_exists($section, $this->sectionContent)) { |
|
| 220 | + $content = $this->sectionContent[$section]; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $context['sections'][$section] = $sectionFactory->create( |
|
| 224 | + $section, |
|
| 225 | + [$section, $content, $this->viewBuilder] |
|
| 226 | + ); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Set the format of the mail. |
|
| 232 | + * |
|
| 233 | + * @since 1.0.0 |
|
| 234 | + * |
|
| 235 | + * @return string Format of the Mail. |
|
| 236 | + */ |
|
| 237 | + protected function getFormat() |
|
| 238 | + { |
|
| 239 | + return $this->format; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Set the format of the mail. |
|
| 244 | + * |
|
| 245 | + * @since 1.0.0 |
|
| 246 | + * |
|
| 247 | + * @return void |
|
| 248 | + */ |
|
| 249 | + abstract protected function setFormat(); |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Set the template to the default template defined in the configuration. |
|
| 253 | + * |
|
| 254 | + * @since 1.0.0 |
|
| 255 | + * |
|
| 256 | + * @throws RuntimeException |
|
| 257 | + */ |
|
| 258 | + protected function setDefaultTemplate() |
|
| 259 | + { |
|
| 260 | + $defaultTemplate = $this->config->getKey('default_template'); |
|
| 261 | + $this->setTemplate($defaultTemplate); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Create an instance of a template. |
|
| 266 | + * |
|
| 267 | + * @since 1.0.0 |
|
| 268 | + * |
|
| 269 | + * @param string $template Template to instantiate. |
|
| 270 | + * |
|
| 271 | + * @return TemplateInterface $template Newly created instance. |
|
| 272 | + * @throws RuntimeException |
|
| 273 | + */ |
|
| 274 | + protected function createTemplate($template) |
|
| 275 | + { |
|
| 276 | + $templateFactory = new Factory($this->config, 'templates'); |
|
| 277 | + |
|
| 278 | + return $templateFactory->create($template, [$template, $this->viewBuilder]); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Set the context of the mail. |
|
| 283 | + * |
|
| 284 | + * @since 1.0.0 |
|
| 285 | + * |
|
| 286 | + * @param array $context Context to set/modify. |
|
| 287 | + * |
|
| 288 | + * @return array Updated context. |
|
| 289 | + */ |
|
| 290 | + abstract protected function setContext(array $context); |
|
| 291 | 291 | } |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | public function getTemplate() |
| 117 | 117 | { |
| 118 | 118 | |
| 119 | - if (! $this->template) { |
|
| 119 | + if ( ! $this->template) { |
|
| 120 | 120 | $this->setDefaultTemplate(); |
| 121 | 121 | } |
| 122 | 122 | |
@@ -152,10 +152,10 @@ discard block |
||
| 152 | 152 | ); |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - if (! $template instanceof TemplateInterface) { |
|
| 155 | + if ( ! $template instanceof TemplateInterface) { |
|
| 156 | 156 | throw new InvalidTemplateException( |
| 157 | 157 | 'Could not set the template, invalid type.', |
| 158 | - (array)$template |
|
| 158 | + (array) $template |
|
| 159 | 159 | ); |
| 160 | 160 | } |
| 161 | 161 | $this->template = $template; |
@@ -29,132 +29,132 @@ |
||
| 29 | 29 | abstract class AbstractTemplate implements TemplateInterface |
| 30 | 30 | { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Configuration Settings. |
|
| 34 | - * |
|
| 35 | - * @since 1.0.0 |
|
| 36 | - * |
|
| 37 | - * @var ConfigInterface |
|
| 38 | - */ |
|
| 39 | - protected $config; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Name of the template. |
|
| 43 | - * |
|
| 44 | - * @since 1.0.0 |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - protected $templateName; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * ViewBuilder to create template and section views. |
|
| 52 | - * |
|
| 53 | - * @since 1.0.0 |
|
| 54 | - * |
|
| 55 | - * @var ViewBuilder |
|
| 56 | - */ |
|
| 57 | - protected $viewBuilder; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Instantiate a AbstractTemplate object. |
|
| 61 | - * |
|
| 62 | - * @since 1.0.0 |
|
| 63 | - * |
|
| 64 | - * @param ConfigInterface $config Configuration settings. |
|
| 65 | - * @param array $arguments Arguments that are passed through the constructor. |
|
| 66 | - * Contained elements: string $template |
|
| 67 | - * |
|
| 68 | - * @throws RuntimeException |
|
| 69 | - */ |
|
| 70 | - public function __construct($config, array $arguments) |
|
| 71 | - { |
|
| 72 | - $this->config = $config; |
|
| 73 | - list($template, $this->viewBuilder) = $arguments; |
|
| 74 | - $this->setTemplateName($template); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get the name of the Template. |
|
| 79 | - * |
|
| 80 | - * @since 1.0.0 |
|
| 81 | - * |
|
| 82 | - * @return string Name of the template. |
|
| 83 | - */ |
|
| 84 | - public function getTemplateName() |
|
| 85 | - { |
|
| 86 | - return $this->templateName; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Set the name of the Template. |
|
| 91 | - * |
|
| 92 | - * @since 1.0.0 |
|
| 93 | - * |
|
| 94 | - * @param string|null $template Optional. Name of the template. |
|
| 95 | - * |
|
| 96 | - * @throws FailedToInitialiseTemplateException If no template name was passed. |
|
| 97 | - * @throws FailedToInitialiseTemplateException If an unknown template name was passed. |
|
| 98 | - */ |
|
| 99 | - protected function setTemplateName($template = null) |
|
| 100 | - { |
|
| 101 | - if (null === $template) { |
|
| 102 | - throw new FailedToInitialiseTemplateException('Initialised template without passing it a template name.'); |
|
| 103 | - } |
|
| 104 | - if (! array_key_exists($template, $this->config['templates'])) { |
|
| 105 | - throw new FailedToInitialiseTemplateException('Initialised template with an unknown template name.'); |
|
| 106 | - } |
|
| 107 | - $this->templateName = $template; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Get an array of Sections that are used by this template. |
|
| 112 | - * |
|
| 113 | - * @since 1.0.0 |
|
| 114 | - * |
|
| 115 | - * @return array Sections that are used by this template. |
|
| 116 | - */ |
|
| 117 | - public function getUsedSections() |
|
| 118 | - { |
|
| 119 | - return $this->config['templates'][$this->getTemplateName()]['sections']; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Render the template for a given context. |
|
| 124 | - * |
|
| 125 | - * @since 1.0.0 |
|
| 126 | - * |
|
| 127 | - * @param array $context The context in which to render the template. |
|
| 128 | - * |
|
| 129 | - * @return string The rendered content. |
|
| 130 | - */ |
|
| 131 | - public function render(array $context) |
|
| 132 | - { |
|
| 133 | - |
|
| 134 | - $viewName = $this->getViewName($context); |
|
| 135 | - $view = $this->viewBuilder->create($viewName); |
|
| 136 | - |
|
| 137 | - $sanitizerType = $this->config['formats'][$context['format']]['sanitizer']; |
|
| 138 | - $sanitizerFactory = new Factory($this->config, 'sanitizers'); |
|
| 139 | - $sanitizer = $sanitizerFactory->create($sanitizerType); |
|
| 140 | - |
|
| 141 | - $output = $view->render($context); |
|
| 142 | - |
|
| 143 | - return $sanitizer->sanitize($output, $context); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get the name of the View to use for rendering. |
|
| 148 | - * |
|
| 149 | - * @since 1.0.0 |
|
| 150 | - * |
|
| 151 | - * @param array $context Context in which to get the view name. |
|
| 152 | - * |
|
| 153 | - * @return string Name of the view. |
|
| 154 | - */ |
|
| 155 | - protected function getViewName(array $context) |
|
| 156 | - { |
|
| 157 | - return $this->config['templates'][$this->getTemplateName()]['view_name'] |
|
| 158 | - . '.' . $context['format']; |
|
| 159 | - } |
|
| 32 | + /** |
|
| 33 | + * Configuration Settings. |
|
| 34 | + * |
|
| 35 | + * @since 1.0.0 |
|
| 36 | + * |
|
| 37 | + * @var ConfigInterface |
|
| 38 | + */ |
|
| 39 | + protected $config; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Name of the template. |
|
| 43 | + * |
|
| 44 | + * @since 1.0.0 |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + protected $templateName; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * ViewBuilder to create template and section views. |
|
| 52 | + * |
|
| 53 | + * @since 1.0.0 |
|
| 54 | + * |
|
| 55 | + * @var ViewBuilder |
|
| 56 | + */ |
|
| 57 | + protected $viewBuilder; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Instantiate a AbstractTemplate object. |
|
| 61 | + * |
|
| 62 | + * @since 1.0.0 |
|
| 63 | + * |
|
| 64 | + * @param ConfigInterface $config Configuration settings. |
|
| 65 | + * @param array $arguments Arguments that are passed through the constructor. |
|
| 66 | + * Contained elements: string $template |
|
| 67 | + * |
|
| 68 | + * @throws RuntimeException |
|
| 69 | + */ |
|
| 70 | + public function __construct($config, array $arguments) |
|
| 71 | + { |
|
| 72 | + $this->config = $config; |
|
| 73 | + list($template, $this->viewBuilder) = $arguments; |
|
| 74 | + $this->setTemplateName($template); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get the name of the Template. |
|
| 79 | + * |
|
| 80 | + * @since 1.0.0 |
|
| 81 | + * |
|
| 82 | + * @return string Name of the template. |
|
| 83 | + */ |
|
| 84 | + public function getTemplateName() |
|
| 85 | + { |
|
| 86 | + return $this->templateName; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Set the name of the Template. |
|
| 91 | + * |
|
| 92 | + * @since 1.0.0 |
|
| 93 | + * |
|
| 94 | + * @param string|null $template Optional. Name of the template. |
|
| 95 | + * |
|
| 96 | + * @throws FailedToInitialiseTemplateException If no template name was passed. |
|
| 97 | + * @throws FailedToInitialiseTemplateException If an unknown template name was passed. |
|
| 98 | + */ |
|
| 99 | + protected function setTemplateName($template = null) |
|
| 100 | + { |
|
| 101 | + if (null === $template) { |
|
| 102 | + throw new FailedToInitialiseTemplateException('Initialised template without passing it a template name.'); |
|
| 103 | + } |
|
| 104 | + if (! array_key_exists($template, $this->config['templates'])) { |
|
| 105 | + throw new FailedToInitialiseTemplateException('Initialised template with an unknown template name.'); |
|
| 106 | + } |
|
| 107 | + $this->templateName = $template; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Get an array of Sections that are used by this template. |
|
| 112 | + * |
|
| 113 | + * @since 1.0.0 |
|
| 114 | + * |
|
| 115 | + * @return array Sections that are used by this template. |
|
| 116 | + */ |
|
| 117 | + public function getUsedSections() |
|
| 118 | + { |
|
| 119 | + return $this->config['templates'][$this->getTemplateName()]['sections']; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Render the template for a given context. |
|
| 124 | + * |
|
| 125 | + * @since 1.0.0 |
|
| 126 | + * |
|
| 127 | + * @param array $context The context in which to render the template. |
|
| 128 | + * |
|
| 129 | + * @return string The rendered content. |
|
| 130 | + */ |
|
| 131 | + public function render(array $context) |
|
| 132 | + { |
|
| 133 | + |
|
| 134 | + $viewName = $this->getViewName($context); |
|
| 135 | + $view = $this->viewBuilder->create($viewName); |
|
| 136 | + |
|
| 137 | + $sanitizerType = $this->config['formats'][$context['format']]['sanitizer']; |
|
| 138 | + $sanitizerFactory = new Factory($this->config, 'sanitizers'); |
|
| 139 | + $sanitizer = $sanitizerFactory->create($sanitizerType); |
|
| 140 | + |
|
| 141 | + $output = $view->render($context); |
|
| 142 | + |
|
| 143 | + return $sanitizer->sanitize($output, $context); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get the name of the View to use for rendering. |
|
| 148 | + * |
|
| 149 | + * @since 1.0.0 |
|
| 150 | + * |
|
| 151 | + * @param array $context Context in which to get the view name. |
|
| 152 | + * |
|
| 153 | + * @return string Name of the view. |
|
| 154 | + */ |
|
| 155 | + protected function getViewName(array $context) |
|
| 156 | + { |
|
| 157 | + return $this->config['templates'][$this->getTemplateName()]['view_name'] |
|
| 158 | + . '.' . $context['format']; |
|
| 159 | + } |
|
| 160 | 160 | } |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | if (null === $template) { |
| 102 | 102 | throw new FailedToInitialiseTemplateException('Initialised template without passing it a template name.'); |
| 103 | 103 | } |
| 104 | - if (! array_key_exists($template, $this->config['templates'])) { |
|
| 104 | + if ( ! array_key_exists($template, $this->config['templates'])) { |
|
| 105 | 105 | throw new FailedToInitialiseTemplateException('Initialised template with an unknown template name.'); |
| 106 | 106 | } |
| 107 | 107 | $this->templateName = $template; |
@@ -155,6 +155,6 @@ discard block |
||
| 155 | 155 | protected function getViewName(array $context) |
| 156 | 156 | { |
| 157 | 157 | return $this->config['templates'][$this->getTemplateName()]['view_name'] |
| 158 | - . '.' . $context['format']; |
|
| 158 | + . '.'.$context['format']; |
|
| 159 | 159 | } |
| 160 | 160 | } |