29
0
mirror of https://github.com/joomla/joomla-cms.git synced 2024-06-20 19:15:27 +00:00

[4.0] Set the cli as global app (#16540)

This commit is contained in:
Allon Moritz 2017-06-29 09:40:06 +02:00 committed by George Wilson
parent 975b0985a8
commit fdd26445c7
16 changed files with 233 additions and 75 deletions

View File

@ -9,6 +9,8 @@
defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Router\Router;
use Joomla\Registry\Registry;
use Joomla\String\StringHelper;
@ -362,7 +364,7 @@ class FinderIndexerHelper
// We need to go to com_languages to get the site default language, it's the best we can guess.
if (empty($lang))
{
$lang = JComponentHelper::getParams('com_languages')->get('site', 'en-GB');
$lang = ComponentHelper::getParams('com_languages')->get('site', 'en-GB');
}
return $lang;
@ -413,10 +415,10 @@ class FinderIndexerHelper
static $router;
// Only get the router once.
if (!($router instanceof JRouter))
if (!($router instanceof Router))
{
// Get and configure the site router.
$router = JFactory::getApplication()->getRouter('site');
$router = Router::getInstance('site');
}
// Build the relative route.

File diff suppressed because one or more lines are too long

View File

@ -200,5 +200,23 @@ class MediawikiCli extends \Joomla\CMS\Application\CliApplication
}
}
// Instantiate the application and execute it
\Joomla\CMS\Application\CliApplication::getInstance('MediawikiCli')->execute();
// Set up the container
JFactory::getContainer()->share(
'MediawikiCli',
function (\Joomla\DI\Container $container)
{
return new MediawikiCli(
null,
null,
null,
null,
$container->get(\Joomla\Event\DispatcherInterface::class),
$container
);
},
true
);
$app = JFactory::getContainer()->get('MediawikiCli');
JFactory::$application = $app;
$app->execute();

View File

@ -22,7 +22,7 @@ if (!defined('_JDEFINES'))
}
// Get the framework.
require_once JPATH_LIBRARIES . '/bootstrap.php';
require_once JPATH_BASE . '/includes/framework.php';
// Configure error reporting to maximum for CLI output.
error_reporting(E_ALL);
@ -64,6 +64,22 @@ class DeletefilesCli extends \Joomla\CMS\Application\CliApplication
}
}
// Instantiate the application object, passing the class name to JCli::getInstance
// and use chaining to execute the application.
\Joomla\CMS\Application\CliApplication::getInstance('DeletefilesCli')->execute();
// Set up the container
JFactory::getContainer()->share(
'DeletefilesCli',
function (\Joomla\DI\Container $container)
{
return new DeletefilesCli(
null,
null,
null,
null,
$container->get(\Joomla\Event\DispatcherInterface::class),
$container
);
},
true
);
$app = JFactory::getContainer()->get('DeletefilesCli');
JFactory::$application = $app;
$app->execute();

View File

@ -36,14 +36,7 @@ if (!defined('_JDEFINES'))
define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR . '/components/com_finder');
// Get the framework.
require_once JPATH_LIBRARIES . '/bootstrap.php';
// Import the configuration.
require_once JPATH_CONFIGURATION . '/configuration.php';
// System configuration.
$config = new JConfig;
define('JDEBUG', $config->debug);
require_once JPATH_BASE . '/includes/framework.php';
// Configure error reporting to maximum for CLI output.
error_reporting(E_ALL);
@ -109,7 +102,6 @@ class FinderCli extends \Joomla\CMS\Application\CliApplication
// Fool the system into thinking we are running as JSite with Smart Search as the active component.
$_SERVER['HTTP_HOST'] = 'domain.com';
JFactory::getApplication('site');
// Purge before indexing if --purge on the command line.
if ($this->input->getString('purge', false))
@ -368,6 +360,22 @@ class FinderCli extends \Joomla\CMS\Application\CliApplication
}
}
// Instantiate the application object, passing the class name to JCli::getInstance
// and use chaining to execute the application.
\Joomla\CMS\Application\CliApplication::getInstance('FinderCli')->execute();
// Set up the container
JFactory::getContainer()->share(
'FinderCli',
function (\Joomla\DI\Container $container)
{
return new FinderCli(
null,
null,
null,
null,
$container->get(\Joomla\Event\DispatcherInterface::class),
$container
);
},
true
);
$app = JFactory::getContainer()->get('FinderCli');
JFactory::$application = $app;
$app->execute();

View File

@ -22,7 +22,7 @@ if (!defined('_JDEFINES'))
}
// Get the framework.
require_once JPATH_LIBRARIES . '/bootstrap.php';
require_once JPATH_BASE . '/includes/framework.php';
/**
* Cron job to trash expired cache data.
@ -44,4 +44,22 @@ class GarbageCron extends \Joomla\CMS\Application\CliApplication
}
}
\Joomla\CMS\Application\CliApplication::getInstance('GarbageCron')->execute();
// Set up the container
JFactory::getContainer()->share(
'GarbageCron',
function (\Joomla\DI\Container $container)
{
return new GarbageCron(
null,
null,
null,
null,
$container->get(\Joomla\Event\DispatcherInterface::class),
$container
);
},
true
);
$app = JFactory::getContainer()->get('GarbageCron');
JFactory::$application = $app;
$app->execute();

View File

@ -31,10 +31,7 @@ if (!defined('_JDEFINES'))
}
// Get the framework.
require_once JPATH_LIBRARIES . '/bootstrap.php';
// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';
require_once JPATH_BASE . '/includes/framework.php';
/**
* This script will fetch the update information for all extensions and store
@ -53,9 +50,6 @@ class Updatecron extends \Joomla\CMS\Application\CliApplication
*/
protected function doExecute()
{
// Load an app which can be used later
JFactory::getApplication('site');
// Get the update cache time
$component = \Joomla\CMS\Component\ComponentHelper::getComponent('com_installer');
@ -71,4 +65,22 @@ class Updatecron extends \Joomla\CMS\Application\CliApplication
}
}
\Joomla\CMS\Application\CliApplication::getInstance('Updatecron')->execute();
// Set up the container
JFactory::getContainer()->share(
'Updatecron',
function (\Joomla\DI\Container $container)
{
return new Updatecron(
null,
null,
null,
null,
$container->get(\Joomla\Event\DispatcherInterface::class),
$container
);
},
true
);
$app = JFactory::getContainer()->get('Updatecron');
JFactory::$application = $app;
$app->execute();

View File

@ -486,7 +486,12 @@ abstract class CMSApplication extends WebApplication implements ContainerAwareIn
throw new \RuntimeException(\JText::sprintf('JLIB_APPLICATION_ERROR_APPLICATION_LOAD', $name), 500);
}
if ($container && $container->exists($classname))
if (!$container)
{
$container = \JFactory::getContainer();
}
if ($container->exists($classname))
{
static::$instances[$name] = $container->get($classname);
}

View File

@ -67,7 +67,7 @@ abstract class CliApplication extends AbstractCliApplication implements Dispatch
$this->close();
}
$container = $container ?: new Container;
$container = $container ?: \JFactory::getContainer();
$this->setContainer($container);
$this->input = $input ?: new \JInputCli;
@ -90,6 +90,9 @@ abstract class CliApplication extends AbstractCliApplication implements Dispatch
// Set the current directory.
$this->set('cwd', getcwd());
// Set up the environment
$this->input->set('format', 'cli');
}
/**
@ -101,7 +104,8 @@ abstract class CliApplication extends AbstractCliApplication implements Dispatch
*
* @return CliApplication
*
* @since 11.1
* @since 11.1
* @deprecated 5.0 Load the app through the container
* @throws \RuntimeException
*/
public static function getInstance($name = null)

View File

@ -159,7 +159,7 @@ abstract class DaemonApplication extends CliApplication
\JLog::add('Received signal: ' . $signal, \JLog::DEBUG);
// Let's make sure we have an application instance.
if (!is_subclass_of(static::$instance, 'CliApplication'))
if (!is_subclass_of(static::$instance, CliApplication::class))
{
\JLog::add('Cannot find the application instance.', \JLog::EMERGENCY);
throw new \RuntimeException('Cannot find the application instance.');

View File

@ -0,0 +1,68 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\CMS\Error\Renderer;
use Joomla\CMS\Error\AbstractRenderer;
/**
* Cli error renderer
*
* @since __DEPLOY_VERSION__
*/
class CliRenderer extends AbstractRenderer
{
/**
* The format (type)
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $type = 'cli';
/**
* Render the error for the given object.
*
* @param \Throwable|\Exception $error The error object to be rendered
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
protected function doRender($error)
{
$buffer = PHP_EOL . 'Error occurred: ' . $error->getMessage() . PHP_EOL . $this->getTrace($error);
if ($prev = $error->getPrevious())
{
$buffer .= PHP_EOL . PHP_EOL . 'Previous Exception: ' . $prev->getMessage() . PHP_EOL . $this->getTrace($prev);
}
return $buffer;
}
/**
* Returns a trace for the given error.
*
* @param \Throwable|\Exception $error The error
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
private function getTrace($error)
{
// Include the stack trace only if in debug mode
if (!JDEBUG)
{
return '';
}
return PHP_EOL . $error->getTraceAsString() . PHP_EOL;
}
}

View File

@ -32,6 +32,7 @@ class ExceptionHandler
{
$expectedClass = PHP_MAJOR_VERSION >= 7 ? '\Throwable' : '\Exception';
$isException = $error instanceof $expectedClass;
$isCli = false;
// In PHP 5, the $error object should be an instance of \Exception; PHP 7 should be a Throwable implementation
if ($isException)
@ -63,8 +64,11 @@ class ExceptionHandler
$app = \JFactory::getApplication();
// Flag if we are on cli
$isCli = \JFactory::getApplication()->isClient('cli');
// If site is offline and it's a 404 error, just go to index (to see offline message, instead of 404)
if ($error->getCode() == '404' && $app->get('offline') == 1)
if (!$isCli && $error->getCode() == '404' && $app->get('offline') == 1)
{
$app->redirect('index.php');
}
@ -78,8 +82,7 @@ class ExceptionHandler
*/
if (\JFactory::$document)
{
// We're probably in an CLI environment
$format = \JFactory::getDocument()->getType();
$format = \JFactory::$document->getType();
}
else
{
@ -98,20 +101,25 @@ class ExceptionHandler
$data = $renderer->render($error);
// Do not allow cache
$app->allowCache(false);
// If nothing was rendered, just use the message from the Exception
if (empty($data))
{
$data = $error->getMessage();
}
$app->setBody($data);
if ($isCli)
{
echo $data;
}
else
{
// Do not allow cache
$app->allowCache(false);
echo $app->toString();
$app->setBody($data);
$app->close(0);
echo $app->toString();
}
// This return is needed to ensure the test suite does not trigger the non-Exception handling below
return;
@ -127,7 +135,7 @@ class ExceptionHandler
}
// This isn't an Exception, we can't handle it.
if (!headers_sent())
if (!$isCli && !headers_sent())
{
header('HTTP/1.1 500 Internal Server Error');
}

View File

@ -116,7 +116,7 @@ class LanguageHelper
if (empty($languages))
{
// Installation uses available languages
if (\JFactory::getApplication()->getClientId() == 2)
if (\JFactory::getApplication()->isClient('installation'))
{
$languages[$key] = array();
$knownLangs = self::getKnownLanguages(JPATH_BASE);

View File

@ -10,7 +10,7 @@ namespace Joomla\CMS\Mvc\Factory;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Application\CmsApplication;
use Joomla\CMS\Application\CMSApplicationInterface;
/**
* Factory to create MVC objects based on a namespace.
@ -29,7 +29,7 @@ class MvcFactory implements MvcFactoryInterface
/**
* The application.
*
* @var CmsApplication
* @var CMSApplicationInterface
*/
private $application = null;
@ -37,12 +37,12 @@ class MvcFactory implements MvcFactoryInterface
* The namespace must be like:
* Joomla\Component\Content
*
* @param string $namespace The namespace.
* @param CmsApplication $application The application
* @param string $namespace The namespace.
* @param CMSApplicationInterface $application The application
*
* @since __DEPLOY_VERSION__
*/
public function __construct($namespace, CmsApplication $application)
public function __construct($namespace, CMSApplicationInterface $application)
{
$this->namespace = $namespace;
$this->application = $application;

View File

@ -12,7 +12,9 @@ namespace Joomla\CMS\Service\Provider;
defined('JPATH_PLATFORM') or die;
use JFactory;
use JLog;
use Joomla\CMS\Application\AdministratorApplication;
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Log\Log;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
@ -38,7 +40,7 @@ class Application implements ServiceProviderInterface
'JApplicationAdministrator',
function (Container $container)
{
$app = new \JApplicationAdministrator(null, null, null, $container);
$app = new AdministratorApplication(null, null, null, $container);
// The session service provider needs JFactory::$application, set it if still null
if (JFactory::$application === null)
@ -47,7 +49,7 @@ class Application implements ServiceProviderInterface
}
$app->setDispatcher($container->get('Joomla\Event\DispatcherInterface'));
$app->setLogger(JLog::createDelegatedLogger());
$app->setLogger(Log::createDelegatedLogger());
$app->setSession($container->get('Joomla\Session\SessionInterface'));
return $app;
@ -59,7 +61,7 @@ class Application implements ServiceProviderInterface
'JApplicationSite',
function (Container $container)
{
$app = new \JApplicationSite(null, null, null, $container);
$app = new SiteApplication(null, null, null, $container);
// The session service provider needs JFactory::$application, set it if still null
if (JFactory::$application === null)
@ -68,7 +70,7 @@ class Application implements ServiceProviderInterface
}
$app->setDispatcher($container->get('Joomla\Event\DispatcherInterface'));
$app->setLogger(JLog::createDelegatedLogger());
$app->setLogger(Log::createDelegatedLogger());
$app->setSession($container->get('Joomla\Session\SessionInterface'));
return $app;

View File

@ -52,9 +52,10 @@ class Session implements ServiceProviderInterface
function (Container $container)
{
$config = JFactory::getConfig();
$app = JFactory::getApplication();
// Generate a session name.
$name = ApplicationHelper::getHash($config->get('session_name', get_class(JFactory::getApplication())));
$name = ApplicationHelper::getHash($config->get('session_name', get_class($app)));
// Calculate the session lifetime.
$lifetime = (($config->get('lifetime')) ? $config->get('lifetime') * 60 : 900);
@ -65,23 +66,14 @@ class Session implements ServiceProviderInterface
'expire' => $lifetime
);
switch (JFactory::getApplication()->getClientId())
if ($app->isClient('site') && $config->get('force_ssl') == 2)
{
case 0:
if ($config->get('force_ssl') == 2)
{
$options['force_ssl'] = true;
}
$options['force_ssl'] = true;
}
break;
case 1:
if ($config->get('force_ssl') >= 1)
{
$options['force_ssl'] = true;
}
break;
if ($app->isClient('administrator') && $config->get('force_ssl') >= 1)
{
$options['force_ssl'] = true;
}
// Set up the storage handler
@ -206,9 +198,9 @@ class Session implements ServiceProviderInterface
throw new InvalidArgumentException(sprintf('The "%s" session handler is not recognised.', $handlerType));
}
$input = JFactory::getApplication()->input;
$input = $app->input;
if (JFactory::getApplication()->isCli())
if ($app->isClient('cli'))
{
$storage = new RuntimeStorage;
}
@ -218,7 +210,11 @@ class Session implements ServiceProviderInterface
}
$dispatcher = $container->get('Joomla\Event\DispatcherInterface');
$dispatcher->addListener('onAfterSessionStart', array(JFactory::getApplication(), 'afterSessionStart'));
if (method_exists($app, 'afterSessionStart'))
{
$dispatcher->addListener('onAfterSessionStart', array($app, 'afterSessionStart'));
}
$session = new \Joomla\CMS\Session\Session($storage, $dispatcher, $options);
$session->addValidator(new AddressValidator($input, $session));