-
Notifications
You must be signed in to change notification settings - Fork 1
/
hydrastic.php
102 lines (90 loc) · 3.74 KB
/
hydrastic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* This file is part of the Hydrastic package.
*
* (c) Baptiste Pizzighini <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
require_once __DIR__.'/autoload.php';
use Symfony\Component\Console\Application;
use Hydrastic\Taxonomy;
use Hydrastic\Command\Process;
use Hydrastic\Command\Compile;
use Hydrastic\Command\Shell;
use Hydrastic\Command\Version;
use Hydrastic\Command\Init;
use Hydrastic\Command\Watch;
use Hydrastic\Service\Twig as TwigService;
use Hydrastic\Service\Yaml as YamlService;
use Hydrastic\Service\Finder as FinderService;
use Hydrastic\Service\Util as UtilService;
use Hydrastic\Service\TextProcessor as TextProcessorService;
use Hydrastic\Service\Logger as LoggerService;
use Hydrastic\Service\Asset as AssetService;
// "Dic" stands for Dependency Injection Container
// It holds configuration variables and services
// Services are utility object lazily loaded throughout the application
$dic = new Pimple();
// Register services that do not depends on config
$dic['yaml'] = $dic->share(function ($c) { return new YamlService($c); });
$dic['util'] = $dic->share(function ($c) { return new UtilService($c); });
// Set the working directory correctly and read/set the conf
// depending on being in a phar archive or not.
$workingDir = dirname(Phar::running(false));
if ($workingDir == '') {
//Currently outside a phar archive
$dic['inside_phar'] = false;
$dic['working_directory'] = dirname(__DIR__);
$dic['hydrastic_dir'] = __DIR__;
$defaultConf = $dic['yaml']['parser']->parse(file_get_contents(__DIR__.'/hydrastic-default-conf.yml'));
$userConfFile = $dic['working_directory'].'/hydrastic-conf.yml';
if (file_exists($userConfFile)) {
$userConf = $dic['yaml']['parser']->parse(file_get_contents($userConfFile));
} else {
$userConf = array();
}
} else {
//Currently inside a phar archive
$dic['inside_phar'] = true;
$dic['working_directory'] = str_replace(array('phar:/','hydrastic.phar'), '', $workingDir);
$dic['hydrastic_dir'] = Phar::running();
$userConfFile = $workingDir.'/hydrastic-conf.yml';
if (file_exists($userConfFile)) {
Phar::mount('hydrastic-conf.yml', $userConfFile);
$userConf = $dic['yaml']['parser']->parse(file_get_contents('hydrastic-conf.yml'));
$dic['user_conf_defined'] = true;
} else {
$dic['user_conf_defined'] = false;
$userConf = array();
}
$defaultConf = $dic['yaml']['parser']->parse(file_get_contents(__DIR__.'/hydrastic-default-conf.yml'));
}
$dic['conf'] = $dic['util']['array.merger']->mergeUniqueKeysRecursive($defaultConf, $userConf);
// Register Taxonomy
$dic['taxonomy'] = $dic->share(function ($c) { return new Taxonomy($c); });
// Register services
$dic['twig'] = $dic->share(function ($c) { return new TwigService($c); });
$dic['finder'] = $dic->share(function ($c) { return new FinderService($c); });
$dic['txt_extensions_registered'] = array();
$dic['textprocessor'] = $dic->share(function ($c) { return new TextProcessorService($c); });
$dic['logger'] = $dic->share(function ($c) { return new LoggerService($c); });
$dic['asset'] = $dic->share(function ($c) { return new AssetService($c); });
// Declare (Symfony Component) Application
$dic['hydrastic_app'] = new Application('Hydrastic',$dic['conf']['version']);
// Add commands to the Application object
$commands = array(
new Process($dic),
new Shell($dic),
new Version($dic),
new Init($dic),
new Watch($dic),
);
if (false === $dic['inside_phar']) {
$commands[] = new Compile($dic);
}
$dic['hydrastic_app']->addCommands($commands);
//Run the Application
$dic['hydrastic_app']->run();