/** * Register Mailer Component * @throws BootstrapException */ protected function registerMailer() { // Container has Mailer component, must be defined in config if (!property_exists($this->config->app, "mailer")) { throw BootstrapException::mailerNode(); } $mailerConfig = $this->config->app->mailer; if (!property_exists($mailerConfig, "agent") || !is_string($mailerConfig->agent)) { throw BootstrapException::mailerAgent(""); } switch (strtolower($mailerConfig->agent)) { case "sendmail": $useSMTP = false; break; case "smtp": $useSMTP = true; break; default: throw BootstrapException::mailerAgent($mailerConfig->agent); } $this->mailer = $this->container->get("Mailer"); // Grab Mailer Instance // Configure default sender name and email $senderName = $mailerConfig->senderName ?? null; if (is_string($senderName)) { $this->mailer->senderName($senderName); } $senderEmail = $mailerConfig->senderEmail ?? null; if (is_string($senderEmail)) { $this->mailer->senderEmail($senderEmail); } // Configure SMTP if ($useSMTP) { $smtpHost = $mailerConfig->host ?? "127.0.0.1"; $smtpPort = intval($mailerConfig->port ?? 25); $smtpTimeout = intval($mailerConfig->timeOut ?? 1); $smtpUsername = $mailerConfig->username ?? ""; $smtpPassword = $mailerConfig->password ?? ""; $smtpTLS = $mailerConfig->useTls ?? true; $smtpTLS = $smtpTLS === false ? false : true; $smtpServerName = $mailerConfig->serverName ?? null; $smtp = (new Mailer\SMTP($smtpHost, $smtpPort, $smtpTimeout))->useTLS($smtpTLS); if (!empty($smtpUsername) && !empty($smtpPassword)) { $smtp->authCredentials((string) $smtpUsername, (string) $smtpPassword); } if (!empty($smtpServerName) && is_string($smtpServerName)) { $smtp->serverName($smtpServerName); } } }
/** * Creates new instance of service * * @param Container $container * @param array $args * @return mixed * @throws ContainerException */ public function createInstance(Container $container, array $args) { // Prepare arguments for constructor $constructorArgs = []; foreach ($this->args as $arg) { $constructorArgs[] = $container->get($arg); } // Merge arguments foreach ($args as $arg) { array_push($constructorArgs, $arg); } // Construct $class = $this->className; /** @var $instance object */ $instance = new $class(...$constructorArgs); // Call setter methods foreach ($this->methods as $method => $di) { if (!is_callable([$instance, $method])) { // Setter method doesn't exist or isn't publicly callable throw ContainerException::injectMethodNotFound(__METHOD__, $class, $method, $di); } // Call method, inject dependency call_user_func_array([$instance, $method], [$container->get($di)]); } // Inject public properties $publicProperties = get_object_vars($instance); foreach ($this->properties as $property => $di) { // Check if property is public if (!in_array($property, $publicProperties)) { throw ContainerException::injectPropertyNotFound(__METHOD__, $class, $property, $di); } // Inject public property $instance->{$property} = $container->get($di); } // Return created instance return $instance; }