CoreFiles by mySites.guru
MVCFactory.php161 lines, 4,584 bytes, from Joomla_6.2.0-alpha2-Alpha-Full_Package.zip
Raw History Compare with 6.2.0-alpha1
md5
f97fa865c6afb54dc47f04fe7fb60ef2
sha1
eac5cd1898cb4489197ae9101456f5c1381ff80a
sha256
d2fcf1b43da85af626895bacdc198b32a0b8766504e95d0f148466db503a23fd
<?php

/**
 * Joomla! Content Management System
 *
 * @copyright  (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\Extension\Service\Provider;

use Joomla\CMS\Cache\CacheControllerFactoryAwareInterface;
use Joomla\CMS\Cache\CacheControllerFactoryInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormFactoryAwareInterface;
use Joomla\CMS\Form\FormFactoryInterface;
use Joomla\CMS\Language\LanguageFactoryAwareInterface;
use Joomla\CMS\Language\LanguageFactoryInterface;
use Joomla\CMS\Mail\MailerFactoryAwareInterface;
use Joomla\CMS\Mail\MailerFactoryInterface;
use Joomla\CMS\MVC\Factory\ApiMVCFactory;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Router\SiteRouter;
use Joomla\CMS\Router\SiteRouterAwareInterface;
use Joomla\CMS\User\UserFactoryAwareInterface;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherAwareInterface;
use Joomla\Event\DispatcherInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
 * Service provider for the service MVC factory.
 *
 * @since  4.0.0
 */
class MVCFactory implements ServiceProviderInterface
{
    /**
     * The extension namespace
     *
     * @var  string
     *
     * @since   4.0.0
     */
    private $namespace;

    /**
     * MVCFactory constructor.
     *
     * @param   string  $namespace  The namespace
     *
     * @since   4.0.0
     */
    public function __construct(string $namespace)
    {
        $this->namespace = $namespace;
    }

    /**
     * Registers the service provider with a DI container.
     *
     * @param   Container  $container  The DI container.
     *
     * @return  void
     *
     * @since   4.0.0
     */
    public function register(Container $container)
    {
        $container->set(
            MVCFactoryInterface::class,
            function (Container $container) {
                $factory = $this->createMVCFactory();

                $this->injectServicesIntoFactory($factory, $container);

                return $factory;
            }
        );
    }

    /**
     * Return component namespace
     *
     * @return  string
     *
     * @since   6.2.0
     */
    protected function getNamespace(): string
    {
        return $this->namespace;
    }

    /**
     * Create MVC Factory
     *
     * @return  MVCFactoryInterface
     *
     * @since   6.2.0
     */
    protected function createMVCFactory(): MVCFactoryInterface
    {
        if (Factory::getApplication()->isClient('api')) {
            return new ApiMVCFactory($this->namespace);
        }

        return new \Joomla\CMS\MVC\Factory\MVCFactory($this->namespace);
    }

    /**
     * Inject services from container into MVC Factory
     *
     * @param   MVCFactoryInterface  $factory    The MVC Factory
     * @param   Container            $container  The DI container
     *
     * @return  void
     *
     * @since   6.2.0
     */
    protected function injectServicesIntoFactory(MVCFactoryInterface $factory, Container $container): void
    {
        if ($factory instanceof FormFactoryAwareInterface) {
            $factory->setFormFactory($container->get(FormFactoryInterface::class));
        }

        if ($factory instanceof DispatcherAwareInterface) {
            $factory->setDispatcher($container->get(DispatcherInterface::class));
        }

        if ($factory instanceof DatabaseAwareInterface) {
            $factory->setDatabase($container->get(DatabaseInterface::class));
        }

        if ($factory instanceof SiteRouterAwareInterface) {
            $factory->setSiteRouter($container->get(SiteRouter::class));
        }

        if ($factory instanceof CacheControllerFactoryAwareInterface) {
            $factory->setCacheControllerFactory($container->get(CacheControllerFactoryInterface::class));
        }

        if ($factory instanceof UserFactoryAwareInterface) {
            $factory->setUserFactory($container->get(UserFactoryInterface::class));
        }

        if ($factory instanceof MailerFactoryAwareInterface) {
            $factory->setMailerFactory($container->get(MailerFactoryInterface::class));
        }

        if ($factory instanceof LanguageFactoryAwareInterface) {
            $factory->setLanguageFactory($container->get(LanguageFactoryInterface::class));
        }
    }
}

The same content is in 9 releases, Joomla 6.2.0-alpha2 to 6.2.1-rc1. See them all, or every version of this file.