示例#1
0
/**
 * Get all content types that exists.
 *
 * @param  array $args {
 *   @type bool         $all
 *   @type mixed        $args
 *   @type array|string $id
 *   @type string       $mode
 *   @type array|string $types
 * }
 *
 * @return array
 */
function papi_get_all_content_types(array $args = [])
{
    $default_args = ['all' => true, 'args' => [], 'mode' => 'include', 'types' => []];
    $args = array_merge($default_args, $args);
    if (!is_array($args['types'])) {
        $args['types'] = [$args['types']];
    }
    if (!is_array($args['args'])) {
        $args['args'] = [$args['args']];
    }
    $args['args'] = array_filter($args['args']);
    if (!empty($args['types'])) {
        $args['all'] = false;
    }
    $content_types = [];
    $files = papi()->once(__FUNCTION__, function () {
        return papi_get_all_core_type_files();
    });
    foreach ($files as $file) {
        $content_type = papi_get_content_type($file);
        if (is_null($content_type)) {
            continue;
        }
        // Only content types can be loaded.
        // @codeCoverageIgnoreStart
        if ($content_type instanceof Papi_Content_Type === false) {
            continue;
        }
        // @codeCoverageIgnoreEnd
        if ($content_type->singleton()) {
            if (!empty($content_types)) {
                continue;
            }
        }
        $valid_type = in_array($content_type->type, $args['types']);
        $valid_type = $args['mode'] === 'include' ? $valid_type : !$valid_type;
        if ($args['all'] || $valid_type && call_user_func_array([$content_type, 'allowed'], $args['args'])) {
            $content_type->boot();
            $content_types[] = $content_type;
        }
        continue;
    }
    if (is_array($content_types)) {
        usort($content_types, function ($a, $b) {
            return strcmp($a->name, $b->name);
        });
    }
    return papi_sort_order(array_reverse($content_types));
}
示例#2
0
文件: entry.php 项目: nlemoine/papi
/**
 * Get all entry types that exists.
 *
 * @param  array $args {
 *   @type bool         $all
 *   @type mixed        $args
 *   @type bool         $cache
 *   @type array|string $id
 *   @type string       $mode
 *   @type array|string $types
 * }
 *
 * @return array
 */
function papi_get_all_entry_types(array $args = [])
{
    $default_args = ['all' => true, 'args' => [], 'cache' => true, 'mode' => 'include', 'types' => []];
    $args = array_merge($default_args, $args);
    if (!is_array($args['types'])) {
        $args['types'] = [$args['types']];
    }
    if (!is_array($args['args'])) {
        $args['args'] = [$args['args']];
    }
    $args['args'] = array_filter($args['args']);
    if (!empty($args['types'])) {
        $args['all'] = false;
    }
    $cache_key = papi_cache_key(__FUNCTION__, md5(serialize($args)));
    if (!$args['cache']) {
        papi()->remove('papi_get_all_core_type_files');
        papi()->remove($cache_key);
    }
    if (papi()->exists($cache_key)) {
        return papi()->make($cache_key);
    }
    $singletons = [];
    $entry_types = [];
    $files = papi_get_all_core_type_files();
    foreach ($files as $file) {
        $entry_type = papi_get_entry_type($file);
        if (is_null($entry_type)) {
            continue;
        }
        // Only entry types can be loaded.
        // @codeCoverageIgnoreStart
        if ($entry_type instanceof Papi_Entry_Type === false) {
            continue;
        }
        // @codeCoverageIgnoreEnd
        if ($entry_type->singleton()) {
            if (isset($singletons[$entry_type->type])) {
                continue;
            } else {
                $singletons[$entry_type->type] = true;
            }
        }
        $id = $entry_type->get_id();
        $valid_type = in_array($entry_type->type, $args['types'], true);
        $valid_type = $args['mode'] === 'include' ? $valid_type : !$valid_type;
        if ($args['all'] || $valid_type && call_user_func_array([$entry_type, 'allowed'], $args['args'])) {
            $entry_type->boot();
            $entry_types[$id] = $entry_type;
        }
    }
    $entry_types = array_values($entry_types);
    if (is_array($entry_types)) {
        usort($entry_types, function ($a, $b) {
            return strcmp($a->name, $b->name);
        });
    }
    $entry_types = papi_sort_order(array_reverse($entry_types));
    if (!papi()->exists($cache_key)) {
        papi()->singleton($cache_key, $entry_types);
    }
    return $entry_types;
}