Merge pull request #23897 from agh1/5.51.0-releasenotes-initial
[civicrm-core.git] / Civi / Test / ExampleDataLoader.php
CommitLineData
2cc4b0c0
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12namespace Civi\Test;
13
652a831d
TO
14use Civi\Core\ClassScanner;
15
2cc4b0c0
TO
16class ExampleDataLoader {
17
18 /**
19 * These are "heavy" properties which are not cached. i.e.
20 * - They are generated by `$ex->build($example);`
21 * - They are not generated by '$ex->getExamples();'
22 * - They are returned by `$this->getFull()`
23 * - They are not returned by `$this->getMeta()`.
24 */
25 const HEAVY_FIELDS = 'data,asserts';
26
27 /**
28 * @var array|null
29 */
30 private $metas;
31
32 /**
33 * Get a list of all examples, including basic metadata (name, title, workflow).
34 *
35 * @return array
36 * Ex: ['my_example' => ['title' => ..., 'workflow' => ..., 'tags' => ...]]
37 * @throws \ReflectionException
38 */
39 public function getMetas(): array {
40 if ($this->metas === NULL) {
41 // $cache = new \CRM_Utils_Cache_NoCache([]);
42 $cache = \CRM_Utils_Constant::value('CIVICRM_TEST') ? new \CRM_Utils_Cache_NoCache([]) : \Civi::cache('long');
43 $cacheKey = \CRM_Utils_String::munge(__CLASS__);
44 $this->metas = $cache->get($cacheKey);
45 if ($this->metas === NULL) {
46 $this->metas = $this->findMetas();
47 $cache->set($cacheKey, $this->metas);
48 }
49 }
50 return $this->metas;
51 }
52
53 public function getMeta(string $name): ?array {
54 $all = $this->getMetas();
55 return $all[$name] ?? NULL;
56 }
57
58 /**
59 * @param string $name
60 *
61 * @return array|null
62 */
63 public function getFull(string $name): ?array {
64 $example = $this->getMeta($name);
65 if ($example === NULL) {
66 return NULL;
67 }
68
652a831d 69 $obj = $this->createObj($example['class']);
2cc4b0c0
TO
70 $obj->build($example);
71 return $example;
72 }
73
74 /**
75 * Get a list of all examples, including basic metadata (name, title, workflow).
76 *
77 * @return array
78 * Ex: ['my_example' => ['title' => ..., 'workflow' => ..., 'tags' => ...]]
79 * @throws \ReflectionException
80 */
81 protected function findMetas(): array {
652a831d 82 $classes = ClassScanner::get(['interface' => ExampleDataInterface::class]);
2cc4b0c0
TO
83
84 $all = [];
652a831d
TO
85 foreach ($classes as $class) {
86 $reflClass = new \ReflectionClass($class);
87 $obj = $this->createObj($class);
2cc4b0c0
TO
88 $offset = 0;
89 foreach ($obj->getExamples() as $example) {
652a831d 90 $example['file'] = \CRM_Utils_File::relativize($reflClass->getFileName(), \Civi::paths()->getPath('[civicrm.root]/'));
2cc4b0c0
TO
91 $example['class'] = $class;
92 if (!isset($example['name'])) {
93 $example['name'] = $example['class'] . '#' . $offset;
94 }
95 $all[$example['name']] = $example;
96 $offset++;
97 }
98 }
99
100 return $all;
101 }
102
652a831d 103 private function createObj(?string $class): ExampleDataInterface {
10282496 104 if (!class_exists($class)) {
652a831d 105 throw new \CRM_Core_Exception("Failed to read example (class '{$class}')");
10282496
TO
106 }
107
108 return new $class();
109 }
110
2cc4b0c0 111}