2017-04-06 01:09:27 +00:00
|
|
|
<?php
|
2018-11-12 16:59:15 +00:00
|
|
|
return function (\Azura\Container $di)
|
2018-09-21 18:04:42 +00:00
|
|
|
{
|
2017-04-06 01:09:27 +00:00
|
|
|
// Override Slim handlers.
|
2018-11-12 16:59:15 +00:00
|
|
|
$di['request'] = function (\Azura\Container $di) {
|
2018-02-01 11:49:40 +00:00
|
|
|
return \App\Http\Request::createFromEnvironment($di->get('environment'));
|
|
|
|
};
|
|
|
|
|
2018-11-12 16:59:15 +00:00
|
|
|
$di['response'] = function (\Azura\Container $di) {
|
2018-02-01 11:49:40 +00:00
|
|
|
$headers = new \Slim\Http\Headers(['Content-Type' => 'text/html; charset=UTF-8']);
|
2018-08-21 00:48:03 +00:00
|
|
|
$response = new \App\Http\Response(200, $headers, null);
|
2018-02-01 11:49:40 +00:00
|
|
|
|
|
|
|
return $response->withProtocolVersion($di->get('settings')['httpVersion']);
|
|
|
|
};
|
|
|
|
|
2018-11-12 16:59:15 +00:00
|
|
|
$di['router'] = function(\Azura\Container $container) {
|
|
|
|
$routerCacheFile = $container->get('settings')[\Azura\Settings::SLIM_ROUTER_CACHE_FILE];
|
|
|
|
$router = new \App\Http\Router();
|
|
|
|
$router->setCacheFile($routerCacheFile);
|
|
|
|
$router->setContainer($container);
|
2018-08-21 00:48:03 +00:00
|
|
|
return $router;
|
|
|
|
};
|
|
|
|
|
2018-11-12 16:59:15 +00:00
|
|
|
$di[\App\Http\ErrorHandler::class] = function($di) {
|
|
|
|
return new \App\Http\ErrorHandler(
|
|
|
|
$di[\App\Acl::class],
|
|
|
|
$di[\Monolog\Logger::class],
|
|
|
|
$di['router'],
|
|
|
|
$di[\Azura\Session::class],
|
2019-03-23 01:22:32 +00:00
|
|
|
$di[\Azura\View::class],
|
|
|
|
$di[\App\Service\Sentry::class]
|
2018-11-12 16:59:15 +00:00
|
|
|
);
|
2017-08-17 18:28:48 +00:00
|
|
|
};
|
2018-11-12 16:59:15 +00:00
|
|
|
|
|
|
|
$di->addAlias('phpErrorHandler', \App\Http\ErrorHandler::class);
|
|
|
|
$di->addAlias('errorHandler', \App\Http\ErrorHandler::class);
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2017-04-06 01:09:27 +00:00
|
|
|
$di['notFoundHandler'] = function ($di) {
|
2018-02-09 11:53:17 +00:00
|
|
|
return function (\App\Http\Request $request, \App\Http\Response $response) use ($di) {
|
2018-11-12 16:59:15 +00:00
|
|
|
/** @var \Azura\View $view */
|
|
|
|
$view = $di[\Azura\View::class];
|
2017-04-06 01:09:27 +00:00
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
return $view->renderToResponse($response->withStatus(404), 'system/error_pagenotfound');
|
2017-04-06 01:09:27 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
$di[\App\Entity\Repository\SettingsRepository::class] = function($di) {
|
2018-02-13 02:10:02 +00:00
|
|
|
/** @var \Doctrine\ORM\EntityManager $em */
|
|
|
|
$em = $di[\Doctrine\ORM\EntityManager::class];
|
|
|
|
|
2018-10-14 02:51:25 +00:00
|
|
|
return new \App\Entity\Repository\SettingsRepository(
|
|
|
|
$em,
|
|
|
|
$em->getClassMetadata(\App\Entity\Settings::class)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-12-05 07:15:51 +00:00
|
|
|
$di[\App\Entity\Repository\StationRepository::class] = function($di) {
|
|
|
|
/** @var \Doctrine\ORM\EntityManager $em */
|
|
|
|
$em = $di[\Doctrine\ORM\EntityManager::class];
|
|
|
|
|
|
|
|
return new \App\Entity\Repository\StationRepository(
|
|
|
|
$em,
|
|
|
|
$em->getClassMetadata(\App\Entity\Station::class),
|
|
|
|
$di[\App\Sync\Task\Media::class],
|
|
|
|
$di[\App\Radio\Adapters::class],
|
2019-01-26 20:38:36 +00:00
|
|
|
$di[\App\Radio\Configuration::class],
|
2019-04-09 09:06:44 +00:00
|
|
|
$di[\Azura\Cache::class],
|
|
|
|
$di[\Symfony\Component\Validator\Validator\ValidatorInterface::class]
|
2018-12-05 07:15:51 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
$di[\App\Entity\Repository\StationMediaRepository::class] = function($di) {
|
|
|
|
/** @var \Doctrine\ORM\EntityManager $em */
|
|
|
|
$em = $di[\Doctrine\ORM\EntityManager::class];
|
|
|
|
|
|
|
|
return new \App\Entity\Repository\StationMediaRepository(
|
|
|
|
$em,
|
|
|
|
$em->getClassMetadata(\App\Entity\StationMedia::class),
|
|
|
|
$di[\App\Radio\Filesystem::class]
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-10-14 02:51:25 +00:00
|
|
|
$di[\App\Entity\Repository\StationPlaylistMediaRepository::class] = function($di) {
|
|
|
|
/** @var \Doctrine\ORM\EntityManager $em */
|
|
|
|
$em = $di[\Doctrine\ORM\EntityManager::class];
|
|
|
|
|
|
|
|
return new \App\Entity\Repository\StationPlaylistMediaRepository(
|
|
|
|
$em,
|
|
|
|
$em->getClassMetadata(\App\Entity\StationPlaylistMedia::class),
|
2018-11-12 16:59:15 +00:00
|
|
|
$di[\Azura\Cache::class]
|
2018-10-14 02:51:25 +00:00
|
|
|
);
|
2018-02-13 02:10:02 +00:00
|
|
|
};
|
|
|
|
|
2018-01-22 16:28:41 +00:00
|
|
|
$di[\App\Auth::class] = function ($di) {
|
2017-07-19 00:09:29 +00:00
|
|
|
/** @var \Doctrine\ORM\EntityManager $em */
|
2018-01-22 16:28:41 +00:00
|
|
|
$em = $di[\Doctrine\ORM\EntityManager::class];
|
2017-07-19 00:09:29 +00:00
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
/** @var App\Entity\Repository\UserRepository $user_repo */
|
|
|
|
$user_repo = $em->getRepository(App\Entity\User::class);
|
2017-07-19 00:09:29 +00:00
|
|
|
|
2018-11-12 16:59:15 +00:00
|
|
|
return new \App\Auth($di[\Azura\Session::class], $user_repo);
|
2018-01-22 16:28:41 +00:00
|
|
|
};
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
$di[\App\Acl::class] = function ($di) {
|
2018-02-06 17:55:58 +00:00
|
|
|
/** @var \Doctrine\ORM\EntityManager $em */
|
|
|
|
$em = $di[\Doctrine\ORM\EntityManager::class];
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
/** @var App\Entity\Repository\RolePermissionRepository $permissions_repo */
|
|
|
|
$permissions_repo = $em->getRepository(App\Entity\RolePermission::class);
|
2018-02-06 17:55:58 +00:00
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
return new \App\Acl($permissions_repo);
|
2017-04-06 01:09:27 +00:00
|
|
|
};
|
|
|
|
|
2018-01-22 16:28:41 +00:00
|
|
|
$di[\InfluxDB\Database::class] = function ($di) {
|
2017-07-13 21:44:09 +00:00
|
|
|
$opts = [
|
|
|
|
'host' => (APP_INSIDE_DOCKER) ? 'influxdb' : 'localhost',
|
|
|
|
'port' => 8086,
|
|
|
|
];
|
2017-04-06 01:09:27 +00:00
|
|
|
|
|
|
|
$influx = new \InfluxDB\Client($opts['host'], $opts['port']);
|
|
|
|
|
|
|
|
return $influx->selectDB('stations');
|
|
|
|
};
|
|
|
|
|
2018-01-22 16:28:41 +00:00
|
|
|
$di[\Supervisor\Supervisor::class] = function ($di) {
|
2017-04-06 01:09:27 +00:00
|
|
|
$guzzle_client = new \GuzzleHttp\Client();
|
|
|
|
$client = new \fXmlRpc\Client(
|
2017-07-19 00:09:29 +00:00
|
|
|
'http://' . (APP_INSIDE_DOCKER ? 'stations' : '127.0.0.1') . ':9001/RPC2',
|
2017-04-06 01:09:27 +00:00
|
|
|
new \fXmlRpc\Transport\HttpAdapterTransport(
|
|
|
|
new \Http\Message\MessageFactory\GuzzleMessageFactory(),
|
|
|
|
new \Http\Adapter\Guzzle6\Client($guzzle_client)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$connector = new \Supervisor\Connector\XmlRpc($client);
|
|
|
|
$supervisor = new \Supervisor\Supervisor($connector);
|
|
|
|
|
2017-05-20 20:58:49 +00:00
|
|
|
if (!$supervisor->isConnected()) {
|
2018-11-12 16:59:15 +00:00
|
|
|
throw new \Azura\Exception(sprintf('Could not connect to supervisord.'));
|
2017-05-20 20:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 01:09:27 +00:00
|
|
|
return $supervisor;
|
|
|
|
};
|
2019-04-12 04:46:27 +00:00
|
|
|
|
|
|
|
$di->extend(\Doctrine\ORM\EntityManager::class, function(\Doctrine\ORM\EntityManager $em, \Azura\Container $di) {
|
|
|
|
$event_manager = $em->getEventManager();
|
|
|
|
$event_manager->addEventSubscriber(new \App\Doctrine\Event\StationRequiresRestart);
|
|
|
|
|
|
|
|
return $em;
|
|
|
|
});
|
2018-11-12 16:59:15 +00:00
|
|
|
|
|
|
|
$di->extend(\Azura\View::class, function(\Azura\View $view, \Azura\Container $di) {
|
2018-09-22 11:52:43 +00:00
|
|
|
$view->registerFunction('mailto', function ($address, $link_text = null) {
|
|
|
|
$address = substr(chunk_split(bin2hex(" $address"), 2, ";&#x"), 3, -3);
|
|
|
|
$link_text = $link_text ?? $address;
|
|
|
|
return '<a href="mailto:' . $address . '">' . $link_text . '</a>';
|
|
|
|
});
|
|
|
|
$view->registerFunction('pluralize', function ($word, $num = 0) {
|
|
|
|
if ((int)$num === 1) {
|
|
|
|
return $word;
|
|
|
|
} else {
|
|
|
|
return \Doctrine\Common\Inflector\Inflector::pluralize($word);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$view->registerFunction('truncate', function ($text, $length = 80) {
|
2019-01-31 17:54:17 +00:00
|
|
|
return \App\Utilities::truncateText($text, $length);
|
2018-09-22 11:52:43 +00:00
|
|
|
});
|
2018-11-26 08:16:55 +00:00
|
|
|
$view->registerFunction('truncateUrl', function($url) {
|
2019-01-31 17:54:17 +00:00
|
|
|
return \App\Utilities::truncateUrl($url);
|
2018-11-26 08:16:55 +00:00
|
|
|
});
|
2019-05-10 05:44:47 +00:00
|
|
|
$view->registerFunction('link', function($url, $external = true, $truncate = true) {
|
|
|
|
$url = htmlspecialchars($url, \ENT_QUOTES, 'UTF-8');
|
|
|
|
|
|
|
|
$a = ['href="'.$url.'"'];
|
|
|
|
if ($external) {
|
|
|
|
$a[] = 'target="_blank"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$a_body = ($truncate) ? \App\Utilities::truncateUrl($url) : $url;
|
|
|
|
return '<a '.implode(' ', $a).'>'.$a_body.'</a>';
|
|
|
|
});
|
2018-09-22 11:52:43 +00:00
|
|
|
|
|
|
|
$view->addData([
|
2018-11-12 16:59:15 +00:00
|
|
|
'assets' => $di[\Azura\Assets::class],
|
2018-09-22 11:52:43 +00:00
|
|
|
'auth' => $di[\App\Auth::class],
|
|
|
|
'acl' => $di[\App\Acl::class],
|
|
|
|
'customization' => $di[\App\Customization::class],
|
2018-11-02 05:25:47 +00:00
|
|
|
'version' => $di[\App\Version::class],
|
2018-09-22 11:52:43 +00:00
|
|
|
]);
|
|
|
|
|
2017-04-06 01:09:27 +00:00
|
|
|
return $view;
|
|
|
|
});
|
|
|
|
|
2018-12-30 10:18:48 +00:00
|
|
|
// MaxMind (IP Geolocation database for listener metadata)
|
2018-08-14 23:08:50 +00:00
|
|
|
$di[\MaxMind\Db\Reader::class] = function($di) {
|
2018-07-03 22:51:05 +00:00
|
|
|
$mmdb_path = dirname(APP_INCLUDE_ROOT).'/geoip/GeoLite2-City.mmdb';
|
2018-08-14 23:08:50 +00:00
|
|
|
return new \MaxMind\Db\Reader($mmdb_path);
|
2018-07-03 22:51:05 +00:00
|
|
|
};
|
|
|
|
|
2018-11-12 16:59:15 +00:00
|
|
|
$di->extend(\Azura\EventDispatcher::class, function(\Azura\EventDispatcher $dispatcher, \Azura\Container $di) {
|
|
|
|
if (isset($di[\App\Plugins::class])) {
|
|
|
|
/** @var \App\Plugins $plugins */
|
|
|
|
$plugins = $di[\App\Plugins::class];
|
2018-09-21 12:04:05 +00:00
|
|
|
|
2018-11-12 16:59:15 +00:00
|
|
|
// Register plugin-provided events.
|
|
|
|
$plugins->registerEvents($dispatcher);
|
|
|
|
}
|
2018-09-21 12:04:05 +00:00
|
|
|
|
|
|
|
return $dispatcher;
|
2018-11-12 16:59:15 +00:00
|
|
|
});
|
2018-09-21 12:04:05 +00:00
|
|
|
|
2019-07-10 05:45:10 +00:00
|
|
|
$di->extend(\Azura\Console\Application::class, function(\Azura\Console\Application $console, $di) {
|
|
|
|
/** @var \App\Version $version */
|
|
|
|
$version = $di[\App\Version::class];
|
|
|
|
|
|
|
|
/** @var \Azura\Settings $settings */
|
|
|
|
$settings = $di['settings'];
|
|
|
|
|
|
|
|
$console->setName($settings[\Azura\Settings::APP_NAME].' Command Line Tools ('.$settings[\Azura\Settings::APP_ENV].')');
|
|
|
|
$console->setVersion($version->getVersion());
|
|
|
|
|
|
|
|
return $console;
|
|
|
|
});
|
|
|
|
|
2018-12-30 10:18:48 +00:00
|
|
|
$di[\App\MessageQueue::class] = function($di) {
|
|
|
|
// Build QueueFactory
|
|
|
|
/** @var \Redis $redis */
|
|
|
|
$redis = $di[\Redis::class];
|
|
|
|
$redis->select(4);
|
|
|
|
$driver = new \Bernard\Driver\PhpRedis\Driver($redis);
|
|
|
|
|
2019-02-28 21:26:18 +00:00
|
|
|
$normalizer = new \Normalt\Normalizer\AggregateNormalizer([
|
|
|
|
new \Bernard\Normalizer\EnvelopeNormalizer,
|
|
|
|
new \Symfony\Component\Serializer\Normalizer\PropertyNormalizer
|
|
|
|
]);
|
|
|
|
|
|
|
|
$symfony_serializer = new \Symfony\Component\Serializer\Serializer([$normalizer]);
|
|
|
|
$serializer = new \Bernard\Serializer($normalizer);
|
|
|
|
|
2018-12-30 10:18:48 +00:00
|
|
|
$queue_factory = new \Bernard\QueueFactory\PersistentFactory($driver, $serializer);
|
|
|
|
|
|
|
|
// Event dispatcher
|
|
|
|
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher;
|
|
|
|
|
|
|
|
// Build Producer
|
|
|
|
$producer = new \Bernard\Producer($queue_factory, $dispatcher);
|
|
|
|
|
|
|
|
// Build Consumer
|
|
|
|
$receivers = require __DIR__.'/messagequeue.php';
|
|
|
|
$router = new \Bernard\Router\ReceiverMapRouter($receivers, new \Bernard\Router\ContainerReceiverResolver($di));
|
|
|
|
|
|
|
|
$consumer = new Bernard\Consumer($router, $dispatcher);
|
|
|
|
|
2019-02-27 19:18:12 +00:00
|
|
|
$mq = new \App\MessageQueue(
|
2018-12-30 10:18:48 +00:00
|
|
|
$queue_factory,
|
|
|
|
$producer,
|
2019-02-27 19:18:12 +00:00
|
|
|
$consumer,
|
|
|
|
$di[\Monolog\Logger::class]
|
2018-12-30 10:18:48 +00:00
|
|
|
);
|
2019-02-27 19:18:12 +00:00
|
|
|
|
|
|
|
$dispatcher->addSubscriber($mq);
|
|
|
|
return $mq;
|
2018-12-30 10:18:48 +00:00
|
|
|
};
|
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
//
|
|
|
|
// AzuraCast-specific dependencies
|
|
|
|
//
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
$di[\App\ApiUtilities::class] = function($di) {
|
|
|
|
return new \App\ApiUtilities(
|
2018-04-17 05:21:46 +00:00
|
|
|
$di[\Doctrine\ORM\EntityManager::class],
|
2018-09-30 16:56:05 +00:00
|
|
|
$di['router'],
|
|
|
|
$di[\App\Customization::class]
|
2018-03-17 02:40:34 +00:00
|
|
|
);
|
2018-02-01 11:49:40 +00:00
|
|
|
};
|
|
|
|
|
2018-11-12 16:59:15 +00:00
|
|
|
$di[\Azura\Assets::class] = function ($di) {
|
|
|
|
/** @var \Azura\Config $config */
|
|
|
|
$config = $di[\Azura\Config::class];
|
2018-08-12 06:35:48 +00:00
|
|
|
|
|
|
|
$libraries = $config->get('assets');
|
2017-06-23 04:54:50 +00:00
|
|
|
|
2017-09-24 11:29:19 +00:00
|
|
|
$versioned_files = [];
|
2018-11-12 16:59:15 +00:00
|
|
|
$assets_file = APP_INCLUDE_ROOT.'/web/static/assets.json';
|
2017-09-24 11:29:19 +00:00
|
|
|
if (file_exists($assets_file)) {
|
|
|
|
$versioned_files = json_decode(file_get_contents($assets_file), true);
|
|
|
|
}
|
2017-06-23 04:54:50 +00:00
|
|
|
|
2018-11-12 16:59:15 +00:00
|
|
|
return new \Azura\Assets($libraries, $versioned_files);
|
2017-06-23 04:54:50 +00:00
|
|
|
};
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
$di[\App\Customization::class] = function ($di) {
|
|
|
|
return new \App\Customization(
|
2018-11-12 16:59:15 +00:00
|
|
|
$di['settings'],
|
2018-08-21 00:48:03 +00:00
|
|
|
$di[\App\Entity\Repository\SettingsRepository::class]
|
2018-04-17 05:21:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-11-02 05:25:47 +00:00
|
|
|
$di[\App\Version::class] = function($di) {
|
|
|
|
return new \App\Version(
|
2018-11-20 04:53:15 +00:00
|
|
|
$di[\Azura\Cache::class],
|
|
|
|
$di['settings']
|
2018-11-02 05:25:47 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-03-22 22:07:17 +00:00
|
|
|
$di[\App\Service\Sentry::class] = function($di) {
|
|
|
|
return new \App\Service\Sentry(
|
|
|
|
$di[\App\Entity\Repository\SettingsRepository::class],
|
|
|
|
$di['settings'],
|
2019-03-23 01:22:32 +00:00
|
|
|
$di[\App\Version::class],
|
|
|
|
$di[\GuzzleHttp\Client::class]
|
2019-03-22 22:07:17 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-07-26 04:54:25 +00:00
|
|
|
$di[\App\Service\NChan::class] = function($di) {
|
|
|
|
return new \App\Service\NChan(
|
|
|
|
$di[\GuzzleHttp\Client::class]
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-04-09 09:06:44 +00:00
|
|
|
$di[\App\Validator\Constraints\StationPortCheckerValidator::class] = function($di) {
|
|
|
|
return new \App\Validator\Constraints\StationPortCheckerValidator(
|
|
|
|
$di[\App\Radio\Configuration::class]
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-05-29 02:46:10 +00:00
|
|
|
// Radio management
|
2018-08-27 07:14:05 +00:00
|
|
|
$di->register(new \App\Provider\RadioProvider);
|
2018-05-01 07:56:38 +00:00
|
|
|
|
2018-05-29 02:46:10 +00:00
|
|
|
// Synchronization tasks
|
2018-08-27 07:14:05 +00:00
|
|
|
$di->register(new \App\Provider\SyncProvider);
|
2018-02-12 12:46:03 +00:00
|
|
|
|
2018-05-29 02:46:10 +00:00
|
|
|
// Web Hooks
|
2018-08-27 07:14:05 +00:00
|
|
|
$di->register(new \App\Provider\WebhookProvider);
|
2018-02-13 02:10:02 +00:00
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
// Middleware
|
2018-08-27 07:14:05 +00:00
|
|
|
$di->register(new \App\Provider\MiddlewareProvider);
|
2017-12-16 03:37:42 +00:00
|
|
|
|
2018-11-15 13:41:52 +00:00
|
|
|
// Notifications
|
|
|
|
$di->register(new \App\Provider\NotificationProvider);
|
|
|
|
|
2019-02-18 00:52:01 +00:00
|
|
|
// Forms
|
|
|
|
$di->register(new \App\Provider\FormProvider);
|
|
|
|
|
2018-05-29 02:46:10 +00:00
|
|
|
// Controller groups
|
2018-08-27 07:14:05 +00:00
|
|
|
$di->register(new \App\Provider\AdminProvider);
|
|
|
|
$di->register(new \App\Provider\ApiProvider);
|
|
|
|
$di->register(new \App\Provider\FrontendProvider);
|
|
|
|
$di->register(new \App\Provider\StationsProvider);
|
2017-12-16 03:37:42 +00:00
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
return $di;
|
2018-07-03 22:51:05 +00:00
|
|
|
};
|