Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Console\View\Components;
4
 
5
use Illuminate\Console\OutputStyle;
6
use Illuminate\Console\QuestionHelper;
7
use ReflectionClass;
8
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
991 lars 9
 
148 lars 10
use function Termwind\render;
11
use function Termwind\renderUsing;
12
 
13
abstract class Component
14
{
15
    /**
16
     * The output style implementation.
17
     *
18
     * @var \Illuminate\Console\OutputStyle
19
     */
20
    protected $output;
21
 
22
    /**
23
     * The list of mutators to apply on the view data.
24
     *
25
     * @var array<int, callable(string): string>
26
     */
27
    protected $mutators;
28
 
29
    /**
30
     * Creates a new component instance.
31
     *
32
     * @param  \Illuminate\Console\OutputStyle  $output
33
     * @return void
34
     */
35
    public function __construct($output)
36
    {
37
        $this->output = $output;
38
    }
39
 
40
    /**
41
     * Renders the given view.
42
     *
43
     * @param  string  $view
44
     * @param  \Illuminate\Contracts\Support\Arrayable|array  $data
45
     * @param  int  $verbosity
46
     * @return void
47
     */
48
    protected function renderView($view, $data, $verbosity)
49
    {
50
        renderUsing($this->output);
51
 
52
        render((string) $this->compile($view, $data), $verbosity);
53
    }
54
 
55
    /**
56
     * Compile the given view contents.
57
     *
58
     * @param  string  $view
59
     * @param  array  $data
60
     * @return void
61
     */
62
    protected function compile($view, $data)
63
    {
64
        extract($data);
65
 
66
        ob_start();
67
 
68
        include __DIR__."/../../resources/views/components/$view.php";
69
 
70
        return tap(ob_get_contents(), function () {
71
            ob_end_clean();
72
        });
73
    }
74
 
75
    /**
76
     * Mutates the given data with the given set of mutators.
77
     *
78
     * @param  array<int, string>|string  $data
79
     * @param  array<int, callable(string): string>  $mutators
80
     * @return array<int, string>|string
81
     */
82
    protected function mutate($data, $mutators)
83
    {
84
        foreach ($mutators as $mutator) {
85
            $mutator = new $mutator;
86
 
87
            if (is_iterable($data)) {
88
                foreach ($data as $key => $value) {
89
                    $data[$key] = $mutator($value);
90
                }
91
            } else {
92
                $data = $mutator($data);
93
            }
94
        }
95
 
96
        return $data;
97
    }
98
 
99
    /**
100
     * Eventually performs a question using the component's question helper.
101
     *
102
     * @param  callable  $callable
103
     * @return mixed
104
     */
105
    protected function usingQuestionHelper($callable)
106
    {
107
        $property = with(new ReflectionClass(OutputStyle::class))
108
            ->getParentClass()
109
            ->getProperty('questionHelper');
110
 
111
        $property->setAccessible(true);
112
 
113
        $currentHelper = $property->isInitialized($this->output)
114
            ? $property->getValue($this->output)
115
            : new SymfonyQuestionHelper();
116
 
117
        $property->setValue($this->output, new QuestionHelper);
118
 
119
        try {
120
            return $callable();
121
        } finally {
122
            $property->setValue($this->output, $currentHelper);
123
        }
124
    }
125
}