diff options
author | Frédéric Guillot <fred@kanboard.net> | 2014-11-14 22:44:25 -0500 |
---|---|---|
committer | Frédéric Guillot <fred@kanboard.net> | 2014-11-14 22:44:25 -0500 |
commit | b081288188bb1744c9d7bd075aa5936e0ccbb9c4 (patch) | |
tree | 68008e35eb129f74b9b2a49f6f6076ed02b601c6 /app/ServiceProvider | |
parent | 1487cb27634161ef558c367150213bc7077e4198 (diff) |
Use Pimple instead of Core\Registry and add Monolog for logging
Diffstat (limited to 'app/ServiceProvider')
-rw-r--r-- | app/ServiceProvider/Database.php | 100 | ||||
-rw-r--r-- | app/ServiceProvider/Event.php | 15 | ||||
-rw-r--r-- | app/ServiceProvider/Logging.php | 21 | ||||
-rw-r--r-- | app/ServiceProvider/Mailer.php | 36 |
4 files changed, 172 insertions, 0 deletions
diff --git a/app/ServiceProvider/Database.php b/app/ServiceProvider/Database.php new file mode 100644 index 00000000..75e1f73e --- /dev/null +++ b/app/ServiceProvider/Database.php @@ -0,0 +1,100 @@ +<?php + +namespace ServiceProvider; + +use Pimple\Container; +use Pimple\ServiceProviderInterface; +use PicoDb\Database as Dbal; + +class Database implements ServiceProviderInterface +{ + public function register(Container $container) + { + $container['db'] = $this->getInstance(); + } + + /** + * Setup the database driver and execute schema migration + * + * @return PicoDb\Database + */ + public function getInstance() + { + switch (DB_DRIVER) { + case 'sqlite': + $db = $this->getSqliteInstance(); + break; + + case 'mysql': + $db = $this->getMysqlInstance(); + break; + + case 'postgres': + $db = $this->getPostgresInstance(); + break; + + default: + die('Database driver not supported'); + } + + if ($db->schema()->check(\Schema\VERSION)) { + return $db; + } + else { + $errors = $db->getLogMessages(); + die('Unable to migrate database schema: <br/><br/><strong>'.(isset($errors[0]) ? $errors[0] : 'Unknown error').'</strong>'); + } + } + + /** + * Setup the Sqlite database driver + * + * @return PicoDb\Database + */ + function getSqliteInstance() + { + require_once __DIR__.'/../Schema/Sqlite.php'; + + return new Dbal(array( + 'driver' => 'sqlite', + 'filename' => DB_FILENAME + )); + } + + /** + * Setup the Mysql database driver + * + * @return PicoDb\Database + */ + function getMysqlInstance() + { + require_once __DIR__.'/../Schema/Mysql.php'; + + return new Dbal(array( + 'driver' => 'mysql', + 'hostname' => DB_HOSTNAME, + 'username' => DB_USERNAME, + 'password' => DB_PASSWORD, + 'database' => DB_NAME, + 'charset' => 'utf8', + )); + } + + /** + * Setup the Postgres database driver + * + * @return PicoDb\Database + */ + public function getPostgresInstance() + { + require_once __DIR__.'/../Schema/Postgres.php'; + + return new Dbal(array( + 'driver' => 'postgres', + 'hostname' => DB_HOSTNAME, + 'username' => DB_USERNAME, + 'password' => DB_PASSWORD, + 'database' => DB_NAME, + )); + } +} diff --git a/app/ServiceProvider/Event.php b/app/ServiceProvider/Event.php new file mode 100644 index 00000000..0436aa7b --- /dev/null +++ b/app/ServiceProvider/Event.php @@ -0,0 +1,15 @@ +<?php + +namespace ServiceProvider; + +use Pimple\Container; +use Pimple\ServiceProviderInterface; +use Core\Event as EventDispatcher; + +class Event implements ServiceProviderInterface +{ + public function register(Container $container) + { + $container['event'] = new EventDispatcher; + } +} diff --git a/app/ServiceProvider/Logging.php b/app/ServiceProvider/Logging.php new file mode 100644 index 00000000..9737cadc --- /dev/null +++ b/app/ServiceProvider/Logging.php @@ -0,0 +1,21 @@ +<?php + +namespace ServiceProvider; + +use Pimple\Container; +use Pimple\ServiceProviderInterface; +use Monolog\Logger; +use Monolog\Handler\StreamHandler; +use Monolog\Handler\SyslogHandler; + +class Logging implements ServiceProviderInterface +{ + public function register(Container $container) + { + $logger = new Logger('app'); + $logger->pushHandler(new StreamHandler(__DIR__.'/../../data/debug.log', Logger::DEBUG)); + $logger->pushHandler(new SyslogHandler('kanboard', LOG_USER, Logger::DEBUG)); + + $container['logger'] = $logger; + } +} diff --git a/app/ServiceProvider/Mailer.php b/app/ServiceProvider/Mailer.php new file mode 100644 index 00000000..c82c16f6 --- /dev/null +++ b/app/ServiceProvider/Mailer.php @@ -0,0 +1,36 @@ +<?php + +namespace ServiceProvider; + +use Pimple\Container; +use Pimple\ServiceProviderInterface; +use Swift_SmtpTransport; +use Swift_SendmailTransport; +use Swift_MailTransport; + +class Mailer implements ServiceProviderInterface +{ + public function register(Container $container) + { + $container['mailer'] = $this->getInstance(); + } + + public function getInstance() + { + switch (MAIL_TRANSPORT) { + case 'smtp': + $transport = Swift_SmtpTransport::newInstance(MAIL_SMTP_HOSTNAME, MAIL_SMTP_PORT); + $transport->setUsername(MAIL_SMTP_USERNAME); + $transport->setPassword(MAIL_SMTP_PASSWORD); + $transport->setEncryption(MAIL_SMTP_ENCRYPTION); + break; + case 'sendmail': + $transport = Swift_SendmailTransport::newInstance(MAIL_SENDMAIL_COMMAND); + break; + default: + $transport = Swift_MailTransport::newInstance(); + } + + return $transport; + } +} |