Commit | Line | Data |
---|---|---|
bdf67e28 SL |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | namespace Civi\FlexMailer; | |
12 | ||
13 | use Symfony\Component\DependencyInjection\ContainerBuilder; | |
14 | use Symfony\Component\DependencyInjection\Definition; | |
15 | use Symfony\Component\DependencyInjection\Reference; | |
16 | use Civi\FlexMailer\FlexMailer as FM; | |
17 | ||
18 | /** | |
19 | * Class Services | |
20 | * @package Civi\FlexMailer | |
21 | * | |
22 | * Manage the setup of any services used by FlexMailer. | |
23 | */ | |
24 | class Services { | |
25 | ||
26 | public static function registerServices(ContainerBuilder $container) { | |
27 | $container->addResource(new \Symfony\Component\Config\Resource\FileResource(__FILE__)); | |
28 | ||
29 | $apiOverrides = $container->setDefinition('civi_flexmailer_api_overrides', new Definition('Civi\API\Provider\ProviderInterface'))->setPublic(TRUE); | |
30 | self::applyStaticFactory($apiOverrides, __CLASS__, 'createApiOverrides'); | |
31 | ||
32 | $container->setDefinition('civi_flexmailer_required_fields', new Definition('Civi\FlexMailer\Listener\RequiredFields', array( | |
33 | array( | |
34 | 'subject', | |
35 | 'name', | |
36 | 'from_name', | |
37 | 'from_email', | |
38 | '(body_html|body_text)', | |
39 | ), | |
40 | )))->setPublic(TRUE); | |
41 | $container->setDefinition('civi_flexmailer_required_tokens', new Definition('Civi\FlexMailer\Listener\RequiredTokens', array( | |
42 | array('traditional'), | |
43 | array( | |
44 | 'domain.address' => ts("Domain address - displays your organization's postal address."), | |
45 | 'action.optOutUrl or action.unsubscribeUrl' => array( | |
46 | 'action.optOut' => ts("'Opt out via email' - displays an email address for recipients to opt out of receiving emails from your organization."), | |
47 | 'action.optOutUrl' => ts("'Opt out via web page' - creates a link for recipients to click if they want to opt out of receiving emails from your organization. Alternatively, you can include the 'Opt out via email' token."), | |
48 | 'action.unsubscribe' => ts("'Unsubscribe via email' - displays an email address for recipients to unsubscribe from the specific mailing list used to send this message."), | |
49 | 'action.unsubscribeUrl' => ts("'Unsubscribe via web page' - creates a link for recipients to unsubscribe from the specific mailing list used to send this message. Alternatively, you can include the 'Unsubscribe via email' token or one of the Opt-out tokens."), | |
50 | ), | |
51 | ), | |
52 | )))->setPublic(TRUE); | |
53 | ||
54 | $container->setDefinition('civi_flexmailer_abdicator', new Definition('Civi\FlexMailer\Listener\Abdicator'))->setPublic(TRUE); | |
55 | $container->setDefinition('civi_flexmailer_default_batcher', new Definition('Civi\FlexMailer\Listener\DefaultBatcher'))->setPublic(TRUE); | |
56 | $container->setDefinition('civi_flexmailer_default_composer', new Definition('Civi\FlexMailer\Listener\DefaultComposer'))->setPublic(TRUE); | |
57 | $container->setDefinition('civi_flexmailer_open_tracker', new Definition('Civi\FlexMailer\Listener\OpenTracker'))->setPublic(TRUE); | |
58 | $container->setDefinition('civi_flexmailer_basic_headers', new Definition('Civi\FlexMailer\Listener\BasicHeaders'))->setPublic(TRUE); | |
59 | $container->setDefinition('civi_flexmailer_to_header', new Definition('Civi\FlexMailer\Listener\ToHeader'))->setPublic(TRUE); | |
60 | $container->setDefinition('civi_flexmailer_attachments', new Definition('Civi\FlexMailer\Listener\Attachments'))->setPublic(TRUE); | |
61 | $container->setDefinition('civi_flexmailer_bounce_tracker', new Definition('Civi\FlexMailer\Listener\BounceTracker'))->setPublic(TRUE); | |
62 | $container->setDefinition('civi_flexmailer_default_sender', new Definition('Civi\FlexMailer\Listener\DefaultSender'))->setPublic(TRUE); | |
63 | $container->setDefinition('civi_flexmailer_hooks', new Definition('Civi\FlexMailer\Listener\HookAdapter'))->setPublic(TRUE); | |
64 | $container->setDefinition('civi_flexmailer_test_prefix', new Definition('Civi\FlexMailer\Listener\TestPrefix'))->setPublic(TRUE); | |
65 | ||
66 | $container->setDefinition('civi_flexmailer_html_click_tracker', new Definition('Civi\FlexMailer\ClickTracker\HtmlClickTracker'))->setPublic(TRUE); | |
67 | $container->setDefinition('civi_flexmailer_text_click_tracker', new Definition('Civi\FlexMailer\ClickTracker\TextClickTracker'))->setPublic(TRUE); | |
68 | ||
69 | foreach (self::getListenerSpecs() as $listenerSpec) { | |
70 | $container->findDefinition('dispatcher')->addMethodCall('addListenerService', $listenerSpec); | |
71 | } | |
72 | ||
73 | $container->findDefinition('civi_api_kernel')->addMethodCall('registerApiProvider', array(new Reference('civi_flexmailer_api_overrides'))); | |
74 | } | |
75 | ||
76 | /** | |
77 | * Get a list of listeners required for FlexMailer. | |
78 | * | |
79 | * This is a standalone, private function because we're experimenting | |
80 | * with how exactly to handle the registration -- e.g. via | |
81 | * `registerServices()` or via `registerListeners()`. | |
82 | * | |
83 | * @return array | |
84 | * Arguments to pass to addListenerService($eventName, $callbackSvc, $priority). | |
85 | */ | |
86 | protected static function getListenerSpecs() { | |
87 | $listenerSpecs = array(); | |
88 | ||
89 | $listenerSpecs[] = array(Validator::EVENT_CHECK_SENDABLE, array('civi_flexmailer_abdicator', 'onCheckSendable'), FM::WEIGHT_START); | |
90 | $listenerSpecs[] = array(Validator::EVENT_CHECK_SENDABLE, array('civi_flexmailer_required_fields', 'onCheckSendable'), FM::WEIGHT_MAIN); | |
91 | $listenerSpecs[] = array(Validator::EVENT_CHECK_SENDABLE, array('civi_flexmailer_required_tokens', 'onCheckSendable'), FM::WEIGHT_MAIN); | |
92 | ||
93 | $listenerSpecs[] = array(FM::EVENT_RUN, array('civi_flexmailer_default_composer', 'onRun'), FM::WEIGHT_MAIN); | |
94 | $listenerSpecs[] = array(FM::EVENT_RUN, array('civi_flexmailer_abdicator', 'onRun'), FM::WEIGHT_END); | |
95 | ||
96 | $listenerSpecs[] = array(FM::EVENT_WALK, array('civi_flexmailer_default_batcher', 'onWalk'), FM::WEIGHT_END); | |
97 | ||
98 | $listenerSpecs[] = array(FM::EVENT_COMPOSE, array('civi_flexmailer_basic_headers', 'onCompose'), FM::WEIGHT_PREPARE); | |
99 | $listenerSpecs[] = array(FM::EVENT_COMPOSE, array('civi_flexmailer_to_header', 'onCompose'), FM::WEIGHT_PREPARE); | |
100 | $listenerSpecs[] = array(FM::EVENT_COMPOSE, array('civi_flexmailer_bounce_tracker', 'onCompose'), FM::WEIGHT_PREPARE); | |
101 | $listenerSpecs[] = array(FM::EVENT_COMPOSE, array('civi_flexmailer_default_composer', 'onCompose'), FM::WEIGHT_MAIN - 100); | |
102 | $listenerSpecs[] = array(FM::EVENT_COMPOSE, array('civi_flexmailer_attachments', 'onCompose'), FM::WEIGHT_ALTER); | |
103 | $listenerSpecs[] = array(FM::EVENT_COMPOSE, array('civi_flexmailer_open_tracker', 'onCompose'), FM::WEIGHT_ALTER); | |
104 | $listenerSpecs[] = array(FM::EVENT_COMPOSE, array('civi_flexmailer_test_prefix', 'onCompose'), FM::WEIGHT_ALTER); | |
105 | $listenerSpecs[] = array(FM::EVENT_COMPOSE, array('civi_flexmailer_hooks', 'onCompose'), FM::WEIGHT_ALTER - 100); | |
106 | ||
107 | $listenerSpecs[] = array(FM::EVENT_SEND, array('civi_flexmailer_default_sender', 'onSend'), FM::WEIGHT_END); | |
108 | ||
109 | return $listenerSpecs; | |
110 | } | |
111 | ||
112 | /** | |
113 | * Tap into the API kernel and override some of the core APIs. | |
114 | * | |
115 | * @return \Civi\API\Provider\AdhocProvider | |
116 | */ | |
117 | public static function createApiOverrides() { | |
118 | $provider = new \Civi\API\Provider\AdhocProvider(3, 'Mailing'); | |
119 | // FIXME: stay in sync with upstream perms | |
120 | $provider->addAction('preview', 'access CiviMail', '\Civi\FlexMailer\API\MailingPreview::preview'); | |
121 | return $provider; | |
122 | } | |
123 | ||
124 | /** | |
125 | * Adapter for using factory methods in old+new versions of Symfony. | |
126 | * | |
127 | * @param \Symfony\Component\DependencyInjection\Definition $def | |
128 | * @param string $factoryClass | |
129 | * @param string $factoryMethod | |
130 | * @return \Symfony\Component\DependencyInjection\Definition | |
131 | * @deprecated | |
132 | */ | |
133 | protected static function applyStaticFactory($def, $factoryClass, $factoryMethod) { | |
134 | if (method_exists($def, 'setFactory')) { | |
135 | $def->setFactory(array($factoryClass, $factoryMethod)); | |
136 | } | |
137 | else { | |
138 | $def->setFactoryClass($factoryClass)->setFactoryMethod($factoryMethod); | |
139 | } | |
140 | return $def; | |
141 | } | |
142 | ||
143 | } |