@@ -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 *
@@ -27,10 +51,14 @@ protected function getStub()
2751 */
2852 public function handle ()
2953 {
54+ $ this ->setupSourceFile ();
55+
56+ if ($ this ->sourceFile === false ) { return false ; }
57+
3058 $ name = Str::of ($ this ->getNameInput ());
3159 $ path = Str::of ($ this ->getPath ($ name ));
3260 $ pathRelative = $ path ->after (base_path ())->replace ('\\' , '/ ' )->trim ('/ ' );
33-
61+
3462 $ this ->infoBlock ("Creating {$ name ->replace ('_ ' , ' ' )->title ()} {$ this ->type }" );
3563 $ this ->progressBlock ("Creating view <fg=blue> {$ pathRelative }</> " );
3664
@@ -40,45 +68,49 @@ public function handle()
4068 return false ;
4169 }
4270
43- $ source = null ;
71+ $ this ->makeDirectory ($ path );
72+
73+ if ($ this ->sourceFile ) {
74+ $ this ->files ->copy ($ this ->sourceFile , $ path );
75+ } else {
76+ $ this ->files ->put ($ path , $ this ->buildClass ($ name ));
77+ }
78+
79+ $ this ->closeProgressBlock ();
80+ }
81+
82+ private function setupSourceFile ()
83+ {
4484 if ($ this ->option ('from ' )) {
4585 $ from = $ this ->option ('from ' );
4686 $ namespaces = ViewNamespaces::getFor ($ this ->viewNamespace );
4787 foreach ($ namespaces as $ namespace ) {
4888 $ viewPath = "$ namespace. $ from " ;
89+
4990 if (view ()->exists ($ viewPath )) {
50- $ source = view ($ viewPath )->getPath ();
91+ $ this ->sourceFile = view ($ viewPath )->getPath ();
92+ $ this ->sourceViewNamespace = $ viewPath ;
5193 break ;
5294 }
5395 }
5496
5597 // full or relative file path may be provided
5698 if (file_exists ($ from )) {
57- $ source = realpath ($ from );
99+ $ this -> sourceFile = realpath ($ from );
58100 }
59101 // remove the first slash to make absolute paths relative in unix systems
60102 elseif (file_exists (substr ($ from , 1 ))) {
61- $ source = realpath (substr ($ from , 1 ));
103+ $ this -> sourceFile = realpath (substr ($ from , 1 ));
62104 }
63105
64- if (! $ source ) {
106+ if (! $ this -> sourceFile ) {
65107 $ this ->errorProgressBlock ();
66108 $ this ->note ("$ this ->type ' $ from' does not exist! " , 'red ' );
67109 $ this ->newLine ();
68110
69- return false ;
111+ $ this -> sourceFile = false ;
70112 }
71113 }
72-
73- $ this ->makeDirectory ($ path );
74-
75- if ($ source ) {
76- $ this ->files ->copy ($ source , $ path );
77- } else {
78- $ this ->files ->put ($ path , $ this ->buildClass ($ name ));
79- }
80-
81- $ this ->closeProgressBlock ();
82114 }
83115
84116 /**
0 commit comments