-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe_builder.module
154 lines (138 loc) · 4 KB
/
recipe_builder.module
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Implements hook_init().
*/
function recipe_builder_init() {
global $config_directories;
if (!isset($config_directories['recipe_builder'])) {
// Lookup the private files and set the folder.
$private_files = config_get('system.core', 'file_private_path');
if (empty($private_files)) {
backdrop_set_message('You must set the private files directory to create a Recipe. ' . l(t('Edit filesystem configuration'), 'admin/config/media/file-system'));
return;
}
$path = config_get('system.core', 'file_private_path') . '/recipe_builder/config';
//Make sure the folder exists and is writable.
$writable = file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
if ($writable) {
$config_directories['recipe_builder'] = $path;
}
}
}
/**
* Implements hook_menu().
*/
function recipe_builder_menu() {
$items = [];
$items['admin/structure/recipes/create'] = [
'title' => 'Create Recipe',
'page callback' => 'backdrop_get_form',
'page arguments' => ['recipe_builder_build_form'],
'access arguments' => array('use recipe builder'),
'file' => 'recipe_builder.builder.inc',
'type' => MENU_LOCAL_ACTION,
];
return $items;
}
/**
* Implements hook_permission().
*/
function recipe_builder_permission() {
return [
'use recipe builder' => [
'title' => t('Use recipe builder'),
'description' => t('Create or edit Recipes using the Recipe Builder module.'),
'restrict access' => TRUE,
'warning' => t('People who can use the recipe builder will be able to create new modules.'),
],
];
}
/**
* Implements hook_preprocess_layout().
*/
function recipe_builder_preprocess_layout(&$variables) {
if (state_get('recipe_builder_build_mode', FALSE)) {
backdrop_set_message(l(t('Recipe build mode is active.'), 'admin/structure/recipes/create'), 'info');
}
}
/**
* Implements hook_autoload_info().
*/
function recipe_builder_autoload_info() {
return [
'Recipe' => 'inc/Recipe.php',
];
}
/**
* Checks if the given config file is a default config provided by the given module.
*
* @param string $project
* The name of the project we are checking.
* @param string $config_name
* The name of the config file we are checking for.
*
* @return bool
*/
function _recipe_builder_check_default_config($project, $config_name) {
static $files = [];
if (empty($files[$project])) {
$project_path = NULL;
foreach (array('module', 'theme') as $project_type) {
if ($project_path = backdrop_get_path($project_type, $project)) {
break;
}
}
$project_config_dir = $project_path . '/config';
if (is_dir($project_config_dir)) {
$storage = new ConfigFileStorage($project_config_dir);
$files[$project] = $storage->listAll();
}
else {
$files[$project] = [];
}
}
if (in_array($config_name, $files[$project])) {
return TRUE;
}
return FALSE;
}
function recipe_builder_recipe_save(Recipe $recipe) {
foreach ($recipe->components as $component_name => $component_data) {
// @TODO:
}
// dpm($recipe);
// Create module file.
// Create config directory.
}
/**
* Generate code friendly to the Backdrop .info format from a structured array.
*
* @param mixed $info
* An array or single value to put in a module's .info file.
* @param string[] $parents
* Array of parent keys (internal use only).
*
* @return string
* A code string ready to be written to a module's .info file.
*
* @todo It would be faster to pass a string for $parents.
*/
function recipe_builder_export_info($info, $parents = array()) {
$output = '';
if (is_array($info)) {
foreach ($info as $k => $v) {
$child = $parents;
$child[] = $k;
$output .= recipe_builder_export_info($v, $child);
}
}
elseif (!empty($info) && count($parents)) {
$line = array_shift($parents);
foreach ($parents as $key) {
$line .= is_numeric($key) ? "[]" : "[{$key}]";
}
$line .= " = {$info}\n";
return $line;
}
return $output;
}