@@ -10,6 +10,30 @@ abstract class PublishOrCreateViewBackpackCommand extends GeneratorCommand
1010{
1111 use \Backpack \CRUD \app \Console \Commands \Traits \PrettyCommandOutput;
1212
13+ /**
14+ * The source file to copy from.
15+ */
16+ public ?string $ sourceFile = null ;
17+
18+ /**
19+ * The source file view namespace.
20+ */
21+ public ?string $ sourceViewNamespace = null ;
22+
23+ /**
24+ * Stub file name.
25+ *
26+ * @var string
27+ */
28+ protected $ stub = '' ;
29+
30+ /**
31+ * View Namespace.
32+ *
33+ * @var string
34+ */
35+ protected $ viewNamespace = '' ;
36+
1337 /**
1438 * Get the stub file for the generator.
1539 *
@@ -32,6 +56,12 @@ protected function getStub()
3256 */
3357 public function handle ()
3458 {
59+ $ this ->setupSourceFile ();
60+
61+ if ($ this ->sourceFile === false ) {
62+ return false ;
63+ }
64+
3565 $ name = Str::of ($ this ->getNameInput ());
3666 $ path = Str::of ($ this ->getPath ($ name ));
3767 $ pathRelative = $ path ->after (base_path ())->replace ('\\' , '/ ' )->trim ('/ ' );
@@ -45,45 +75,49 @@ public function handle()
4575 return false ;
4676 }
4777
48- $ source = null ;
78+ $ this ->makeDirectory ($ path );
79+
80+ if ($ this ->sourceFile ) {
81+ $ this ->files ->copy ($ this ->sourceFile , $ path );
82+ } else {
83+ $ this ->files ->put ($ path , $ this ->buildClass ($ name ));
84+ }
85+
86+ $ this ->closeProgressBlock ();
87+ }
88+
89+ private function setupSourceFile ()
90+ {
4991 if ($ this ->option ('from ' )) {
5092 $ from = $ this ->option ('from ' );
5193 $ namespaces = ViewNamespaces::getFor ($ this ->viewNamespace );
5294 foreach ($ namespaces as $ namespace ) {
5395 $ viewPath = "$ namespace. $ from " ;
96+
5497 if (view ()->exists ($ viewPath )) {
55- $ source = view ($ viewPath )->getPath ();
98+ $ this ->sourceFile = view ($ viewPath )->getPath ();
99+ $ this ->sourceViewNamespace = $ viewPath ;
56100 break ;
57101 }
58102 }
59103
60104 // full or relative file path may be provided
61105 if (file_exists ($ from )) {
62- $ source = realpath ($ from );
106+ $ this -> sourceFile = realpath ($ from );
63107 }
64108 // remove the first slash to make absolute paths relative in unix systems
65109 elseif (file_exists (substr ($ from , 1 ))) {
66- $ source = realpath (substr ($ from , 1 ));
110+ $ this -> sourceFile = realpath (substr ($ from , 1 ));
67111 }
68112
69- if (! $ source ) {
113+ if (! $ this -> sourceFile ) {
70114 $ this ->errorProgressBlock ();
71115 $ this ->note ("$ this ->type ' $ from' does not exist! " , 'red ' );
72116 $ this ->newLine ();
73117
74- return false ;
118+ $ this -> sourceFile = false ;
75119 }
76120 }
77-
78- $ this ->makeDirectory ($ path );
79-
80- if ($ source ) {
81- $ this ->files ->copy ($ source , $ path );
82- } else {
83- $ this ->files ->put ($ path , $ this ->buildClass ($ name ));
84- }
85-
86- $ this ->closeProgressBlock ();
87121 }
88122
89123 /**
0 commit comments