View source
<?php
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
function potx_local_init($module_path = NULL) {
global $_potx_found_modules;
global $_potx_schema_lookup;
global $_potx_reverse_lookup_built;
$_potx_found_modules = array();
$_potx_schema_lookup = array();
$_potx_reverse_lookup_built = FALSE;
global $potx_callbacks;
$potx_callbacks = array(
'schema_store_lookup' => '_potx_schema_store_lookup',
'schema_reverse_lookup' => '_potx_schema_reverse_lookup',
'load_module_metadata' => '_potx_load_module_metadata',
'store_module_metadata' => '_potx_store_module_metadata',
'schema_store' => '_potx_schema_store',
'schema_load' => '_potx_schema_load',
);
if ($module_path !== NULL) {
_potx_find_all_modules($module_path);
}
}
function _potx_find_all_modules($module_path) {
global $_potx_found_modules;
$module_path = realpath($module_path);
if (substr($module_path, -1) != '/') {
$module_path = $module_path . '/';
}
$checks = array(
'/sites/',
'/core/',
'/profiles/',
'/modules/',
'/themes/',
);
$search_paths = array();
foreach ($checks as $check) {
if (preg_match("!{$check}!", $module_path)) {
$parts = explode($check, $module_path, 2);
$root = $parts[0];
$_potx_found_modules['core']['path'] = $root . '/core';
$search_paths[] = $root;
$search_paths[] = $root . '/core';
$profiles = glob($root . '/profiles/*', GLOB_ONLYDIR);
$sites = glob($root . '/sites/*', GLOB_ONLYDIR);
if (is_array($profiles)) {
$search_paths = array_merge($search_paths, $profiles);
}
if (is_array($sites)) {
$search_paths = array_merge($search_paths, $sites);
}
break;
}
}
foreach ($search_paths as $search_path) {
foreach (array(
'/modules',
'/themes',
) as $sub) {
if (is_dir($search_path . $sub)) {
_potx_find_modules($search_path . $sub);
}
}
}
}
function _potx_find_modules($path) {
global $_potx_found_modules;
$subdirs = glob($path . '/*', GLOB_ONLYDIR);
if (is_array($subdirs)) {
foreach ($subdirs as $dir) {
if (!preg_match("!(^|.+/)(CVS|\\.svn|\\.git|tests|vendor)\$!", $dir)) {
$module_name = basename($dir);
$info_path = $dir . '/' . $module_name . '.info.yml';
if (file_exists($info_path)) {
$_potx_found_modules[$module_name]['path'] = $dir;
}
_potx_find_modules($dir);
}
}
}
}
function _potx_schema_store_lookup($keys, $module_name) {
global $_potx_schema_lookup;
$_potx_schema_lookup = array_merge($_potx_schema_lookup, array_fill_keys($keys, $module_name));
}
function _potx_schema_reverse_lookup($matching_candidates) {
global $_potx_schema_lookup;
global $_potx_reverse_lookup_built;
if (!$_potx_reverse_lookup_built) {
_potx_build_reverse_lookup();
}
foreach ($matching_candidates as $candidate) {
if (isset($_potx_schema_lookup[$candidate])) {
return $_potx_schema_lookup[$candidate];
}
}
return NULL;
}
function _potx_build_reverse_lookup() {
global $_potx_reverse_lookup_built;
global $_potx_found_modules;
global $potx_callbacks;
foreach ($_potx_found_modules as $module_name => $module_data) {
$module_files = _potx_explore_dir($module_data['path'] . '/', '*', POTX_API_8);
foreach ($module_files as $file_name) {
if (preg_match('~config/schema/[^/]+\\.yml$~', $file_name)) {
$code = file_get_contents($file_name);
try {
$yaml = Yaml::parse($code);
$keys = array_keys($yaml);
$potx_callbacks['schema_store_lookup']($keys, $module_name);
} catch (ParseException $e) {
watchdog('potx', "YAML parseing error on file @path: @error", array(
'@path' => $file_path,
'@error' => $e
->getMessage(),
));
}
}
}
}
$_potx_reverse_lookup_built = TRUE;
}
function _potx_load_module_metadata($module_name) {
global $_potx_found_modules;
global $_potx_module_metadata;
if (!isset($_potx_found_modules[$module_name])) {
return FALSE;
}
$module_path = $_potx_found_modules[$module_name]['path'];
if ($module_name === 'core') {
$_potx_module_metadata['core']['dependencies'] = array();
}
else {
$info_path = $module_path . '/' . $module_name . '.info.yml';
$code = file_get_contents($info_path);
try {
$info_yaml = Yaml::parse($code);
$_potx_module_metadata[$module_name]['dependencies'] = isset($info_yaml['dependencies']) ? $info_yaml['dependencies'] : array();
} catch (ParseException $e) {
watchdog('potx', "YAML parseing error on file @path: @error", array(
'@path' => $file_path,
'@error' => $e
->getMessage(),
));
return FALSE;
}
}
$module_files = _potx_explore_dir($module_path . '/config/schema/', '*', POTX_API_8);
foreach ($module_files as $file_path) {
if (preg_match('~config/schema/[^/]+\\.yml$~', $file_path)) {
$_potx_module_metadata[$module_name]['config']['schema'][] = array(
NULL,
$file_path,
);
}
}
return TRUE;
}
function _potx_store_module_metadata($module_name, $metadata) {
}
function _potx_schema_store($module_name) {
global $_potx_module_schema;
global $_potx_schema_cache;
$_potx_schema_cache[$module_name] = $_potx_module_schema;
}
function _potx_schema_load($module_name) {
global $_potx_schema_cache;
if (isset($_potx_schema_cache[$module_name])) {
return $_potx_schema_cache[$module_name];
}
return NULL;
}