1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Framework; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPApplication; |
6
|
|
|
use SilverStripe\Control\HTTPRequestBuilder; |
7
|
|
|
use SilverStripe\Core\CoreKernel; |
8
|
|
|
use SilverStripe\Core\Startup\ErrorControlChainMiddleware; |
9
|
|
|
use SilverStripe\ORM\DB; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Loads and configures SilverStripe |
13
|
|
|
* |
14
|
|
|
* @package silverstripe-console |
15
|
|
|
* @author Robbie Averill <[email protected]> |
16
|
3 |
|
*/ |
17
|
|
|
class Bootstrap |
18
|
3 |
|
{ |
19
|
|
|
/** |
20
|
|
|
* Ensure SilverStripe is loaded and configured |
21
|
|
|
*/ |
22
|
|
|
public function initialize() |
23
|
3 |
|
{ |
24
|
3 |
|
if (!$this->findSilverStripe()) { |
25
|
|
|
echo 'A SilverStripe installation could not be found. Please run ssconsole from your ' |
26
|
|
|
. 'SilverStripe root.', PHP_EOL; |
27
|
|
|
exit; |
|
|
|
|
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Find and require SilverStripe. This will look in: |
33
|
|
|
* |
34
|
|
|
* - The current working directory (for when installed globally with composer) |
35
|
3 |
|
* - The next directory up (for when installed locally into a SilverStripe project) |
36
|
|
|
* - The console's "silverstripe" directory (for when installed in a build process) |
37
|
3 |
|
* |
38
|
3 |
|
* @return bool |
39
|
3 |
|
*/ |
40
|
3 |
|
protected function findSilverStripe() |
|
|
|
|
41
|
3 |
|
{ |
42
|
3 |
|
if (defined('SILVERSTRIPE_ROOT_DIR')) { |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
foreach ([getcwd(), CONSOLE_BASE_DIR . '/../', CONSOLE_BASE_DIR . '/silverstripe'] as $rootFolder) { |
47
|
|
|
if (file_exists($rootFolder . '/vendor/silverstripe/framework/src/Core/CoreKernel.php')) { |
48
|
|
|
define('SILVERSTRIPE_ROOT_DIR', $rootFolder); |
49
|
|
|
|
50
|
|
|
require_once $rootFolder . '/vendor/autoload.php'; |
51
|
|
|
|
52
|
|
|
$_SERVER['REQUEST_URI'] = '/'; |
53
|
3 |
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
54
|
|
|
|
55
|
3 |
|
// Build request and detect flush |
56
|
3 |
|
$request = HTTPRequestBuilder::createFromEnvironment(); |
57
|
3 |
|
|
58
|
|
|
// Default application |
59
|
3 |
|
$kernel = new CoreKernel(BASE_PATH); |
60
|
|
|
$app = new HTTPApplication($kernel); |
61
|
|
|
$app->addMiddleware(new ErrorControlChainMiddleware($app)); |
62
|
|
|
$response = $app->handle($request); |
|
|
|
|
63
|
|
|
|
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.