-
Notifications
You must be signed in to change notification settings - Fork 5
/
Dependencies.php
84 lines (76 loc) · 2.44 KB
/
Dependencies.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
<?php
/**
* Compiles your service definitions into a single class with getters for each service, validating that all declared
* services can be created.
*
* Options:
*
* * --class-name - the name of the class to generate (default: Dependency_Container)
* * --path - path to output the resulting class file (default: APPPATH/classes/Dependency/Container.php)
* * --config-group - config group to load dependencies from (default: dependencies)
*
* If your service definitions are invalid, the task will fail with an error. The recommended use is to exclude the
* compiled class from your version control system, and compile it fresh for every build/deploy. This ensures it is
* up to date with all your modules and related configuration, and that your build will fail fast if the service
* configuration is not valid.
*
* @author Andrew Coulton <[email protected]>
* @copyright 2014 inGenerator Ltd
* @licence BSD
*/
class Task_Compile_Dependencies extends Minion_Task {
public function __construct()
{
$this->_options = array(
'class-name' => 'Dependency_Container',
'path' => APPPATH.'/classes/Dependency/Container.php',
'config-group' => 'dependencies'
);
parent::__construct();
}
/**
* @param \Validation $validation
* @return \Validation
*/
public function build_validation(Validation $validation)
{
return parent::build_validation($validation)
->rule('class-name', 'not_empty')
->rule('path', 'not_empty')
->rule('path', array($this, '_valid_path'))
->rule('config-group', 'not_empty');
}
/**
* @param array $params
*
* @return void
*/
protected function _execute(array $params)
{
\Minion_CLI::write('Loading dependency list from config');
$definitions = Dependency_Definition_List::factory()
->from_array(Kohana::$config->load($params['config-group'])->as_array());
\Minion_CLI::write('Compiling dependencies to '.$params['class-name'].' in '.$params['path']);
$compiler = new Dependency_Compiler;
$compiler->compile($params['class-name'], $params['path'], $definitions);
\Minion_CLI::write('Done');
}
/**
* Check if the class can be written to the specified absolute path
*
* @param string $path
*
* @return bool
*/
public function _valid_path($path)
{
if (file_exists($path)) {
return is_writable($path);
}
$dir = dirname($path);
if ( ! is_dir($dir)) {
mkdir($dir, 0755, TRUE);
}
return is_writable($dir);
}
}