Subversion-Projekte lars-tiefland.laravel_shop

Revision

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