<?php
namespace Moto\Website; use Moto; use Website; use Zend; class HomePageService { protected $_appInstance = null; protected $_providers = [ 'page' => [ 'label' => 'Page', 'class' => Moto\Website\Application::class, 'hidden' => true, ] ]; public function __construct() { $this->bootstrap(); } protected function bootstrap() { if (Moto\Website\BlogApplication::isAllowedForUsing()) { $this->addProvider('blog', [ 'label' => 'MODULE.SETTINGS.GENERAL.HOME_PAGE_PROVIDER_BLOG_LABEL', 'class' => Moto\Website\BlogApplication::class, ]); } Moto\Hook::trigger(Moto\Hook::HOME_PAGE_SERVICE_BOOTSTRAPPED, $this); } public function getBootedApplication() { if ($this->_appInstance) { return $this->_appInstance; } $this->_appInstance = $this->getApplicationInstance($this->getMainApplicationName()); if (!$this->_appInstance) { return null; } if (method_exists($this->_appInstance, 'bootAsHomePage')) { $this->_appInstance->bootAsHomePage(); } return $this->_appInstance; } public function getMainApplicationName() { return Moto\Util::getValue(Moto\Website\Settings::get('home_page_provider'), 'name'); } public function getApplicationInstance($name = null) { if ($name === null) { $name = $this->getMainApplicationName(); } if (!$this->isExistsProvider($name)) { return null; } $data = Moto\Util::getValue($this->_providers, $name); $instance = Moto\Util::getValue($data, 'instance'); if ($instance) { return $instance; } $class = Moto\Util::getValue($data, 'class'); if (!class_exists($class)) { return null; } $instance = Moto\System::app($class); $this->_providers[$name]['instance'] = $instance; return $instance; } public function isExistsProvider($name) { return array_key_exists($name, $this->_providers); } public function addProvider($name, $options) { if (array_key_exists($name, $this->_providers)) { return false; } if (!is_array($options)) { return false; } if (empty($options['label']) || empty($options['class'])) { return false; } $options['hidden'] = (boolean) Moto\Util::getValue($options, 'hidden'); $this->_providers[$name] = $options; return true; } public function getList() { return array_keys($this->_providers); } public function getPossibleValues($controlName, $propertyName) { if ($controlName === 'home_page_provider' && $propertyName === 'name') { $result = []; foreach ($this->_providers as $name => $options) { if ($options['hidden']) { continue; } $result[] = [ 'value' => $name, 'label' => $options['label'] ]; } return $result; } return null; } } 