1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
5 | * @copyright Aimeos (aimeos.org), 2016 |
||
6 | * @package Slim |
||
7 | * @subpackage Command |
||
8 | */ |
||
9 | |||
10 | namespace Aimeos\Slim\Command; |
||
11 | |||
12 | |||
13 | /** |
||
14 | * Aimeos cache command class |
||
15 | * |
||
16 | * @package Slim |
||
17 | * @subpackage Command |
||
18 | */ |
||
19 | class Jobs extends Base implements Iface |
||
20 | { |
||
21 | /** |
||
22 | * Returns the command usage and options |
||
23 | * |
||
24 | * @return string Command usage and options |
||
25 | */ |
||
26 | public static function usage() : string |
||
27 | { |
||
28 | return "Usage: php job.php [--extdir=<path>]* [--config=<path>|<file>]* [--routes=<file>] \"job1 [job2]*\" [\"sitecode1 [sitecode2]*\"]\n"; |
||
29 | } |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Executes the command |
||
34 | * |
||
35 | * @param array $argv Associative array from $_SERVER['argv'] |
||
36 | */ |
||
37 | public static function run( array $argv ) |
||
38 | { |
||
39 | array_shift( $argv ); |
||
40 | $options = self::getOptions( $argv ); |
||
41 | |||
42 | if( ( $jobs = array_shift( $argv ) ) === null ) { |
||
43 | throw new \Aimeos\Slim\Command\Exception(); |
||
44 | } |
||
45 | $sites = array_shift( $argv ); |
||
46 | |||
47 | $config = self::getConfig( $options ); |
||
48 | |||
49 | $app = new \Slim\App( $config ); |
||
50 | $aimeos = new \Aimeos\Slim\Bootstrap( $app, $config ); |
||
51 | $aimeos->setup( ( isset( $options['extdir'] ) ? $options['extdir'] : './ext' ) ) |
||
52 | ->routes( ( isset( $options['routes'] ) ? $options['routes'] : './src/aimeos-routes.php' ) ); |
||
53 | |||
54 | $container = $app->getContainer(); |
||
55 | $context = self::getContext( $container ); |
||
56 | |||
57 | $siteItems = self::getSiteItems( $context, $sites ); |
||
58 | self::execute( $container->get( 'aimeos' ), $context, $siteItems, $jobs ); |
||
59 | } |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Returns the configuration based on the given options |
||
64 | * |
||
65 | * @param array $options Associative list of given options |
||
66 | * @return array Multi-dimensional array of configuration settings |
||
67 | */ |
||
68 | protected static function getConfig( array $options ) : array |
||
69 | { |
||
70 | $config = array(); |
||
71 | |||
72 | if( isset( $options['config'] ) ) |
||
73 | { |
||
74 | foreach( (array) $options['config'] as $path ) |
||
75 | { |
||
76 | if( is_file( $path ) ) { |
||
77 | $config = array_replace_recursive( $config, require $path ); |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | |||
82 | return $config; |
||
83 | } |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Returns a new context object |
||
88 | * |
||
89 | * @param \Psr\Container\ContainerInterface $container Dependency injection container |
||
90 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
91 | */ |
||
92 | protected static function getContext( \Psr\Container\ContainerInterface $container ) : \Aimeos\MShop\Context\Item\Iface |
||
93 | { |
||
94 | $aimeos = $container->get( 'aimeos' ); |
||
95 | $context = $container->get( 'aimeos.context' )->get( false, array(), 'command' ); |
||
96 | |||
97 | $env = \Slim\Http\Environment::mock(); |
||
98 | $request = \Slim\Http\Request::createFromEnvironment( $env ); |
||
99 | $response = new \Slim\Http\Response(); |
||
100 | |||
101 | $tmplPaths = $aimeos->getCustomPaths( 'controller/jobs/templates' ); |
||
102 | $view = $container->get( 'aimeos.view' )->create( $context, $request, $response, array(), $tmplPaths ); |
||
103 | |||
104 | $langManager = \Aimeos\MShop::create( $context, 'locale/language' ); |
||
105 | $langids = $langManager->searchItems( $langManager->createSearch( true ) )->keys()->toArray(); |
||
106 | $i18n = $container->get( 'aimeos.i18n' )->get( $langids ); |
||
107 | |||
108 | $context->setEditor( 'aimeos:jobs' ); |
||
109 | $context->setView( $view ); |
||
110 | $context->setI18n( $i18n ); |
||
111 | |||
112 | return $context; |
||
113 | } |
||
114 | |||
115 | |||
116 | /** |
||
117 | * Removes the cached data for the given sites |
||
118 | * |
||
119 | * @param \Aimeos\Bootstrap $aimeos Aimeos bootstrap object |
||
120 | * @param \Aimeos\MShop\Context\Item\Iface $ctx Context object |
||
121 | * @param \Aimeos\Map $siteItems List of site items implementing \Aimeos\MShop\Locale\Site\Iface |
||
122 | */ |
||
123 | protected static function execute( \Aimeos\Bootstrap $aimeos, \Aimeos\MShop\Context\Item\Iface $ctx, \Aimeos\Map $siteItems, $jobs ) |
||
124 | { |
||
125 | $process = $ctx->getProcess(); |
||
126 | $localeManager = \Aimeos\MShop::create( $ctx, 'locale' ); |
||
127 | |||
128 | foreach( $siteItems as $siteItem ) |
||
129 | { |
||
130 | $localeItem = $localeManager->bootstrap( $siteItem->getCode(), 'en', '', false ); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
131 | $localeItem->setLanguageId( null ); |
||
132 | $localeItem->setCurrencyId( null ); |
||
133 | |||
134 | $ctx->setLocale( $localeItem ); |
||
135 | |||
136 | printf( "Executing the Aimeos jobs for \"%s\"\n", $siteItem->getCode() ); |
||
137 | |||
138 | foreach( (array) explode( ' ', $jobs ) as $jobname ) |
||
139 | { |
||
140 | $fcn = function( $ctx, $aimeos, $jobname ) { |
||
141 | \Aimeos\Controller\Jobs::create( $ctx, $aimeos, $jobname )->run(); |
||
142 | }; |
||
143 | |||
144 | $process->start( $fcn, [$ctx, $aimeos, $jobname], true ); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | $process->wait(); |
||
149 | } |
||
150 | } |
||
151 |