@@ -49,6 +49,13 @@ class AppendOp extends AbstractOperation {
49
49
*/
50
50
protected $ forceAppend ;
51
51
52
+ /**
53
+ * The contents from the file that we are prepending / appending to.
54
+ *
55
+ * @var string
56
+ */
57
+ protected $ originalContents ;
58
+
52
59
/**
53
60
* Constructs an AppendOp.
54
61
*
@@ -69,16 +76,36 @@ public function __construct(ScaffoldFilePath $prepend_path = NULL, ScaffoldFileP
69
76
$ this ->managed = TRUE ;
70
77
}
71
78
79
+ /**
80
+ * {@inheritdoc}
81
+ */
82
+ protected function generateContents () {
83
+ // Fetch the prepend contents, if provided.
84
+ $ prepend_contents = '' ;
85
+ if (!empty ($ this ->prepend )) {
86
+ $ prepend_contents = file_get_contents ($ this ->prepend ->fullPath ()) . "\n" ;
87
+ }
88
+ // Fetch the append contents, if provided.
89
+ $ append_contents = '' ;
90
+ if (!empty ($ this ->append )) {
91
+ $ append_contents = "\n" . file_get_contents ($ this ->append ->fullPath ());
92
+ }
93
+
94
+ // Get the original contents, or the default data if the original is empty.
95
+ $ original_contents = $ this ->originalContents ;
96
+ if (empty ($ original_contents ) && !empty ($ this ->default )) {
97
+ $ original_contents = file_get_contents ($ this ->default ->fullPath ());
98
+ }
99
+
100
+ // Attach it all together.
101
+ return $ prepend_contents . $ original_contents . $ append_contents ;
102
+ }
103
+
72
104
/**
73
105
* {@inheritdoc}
74
106
*/
75
107
public function process (ScaffoldFilePath $ destination , IOInterface $ io , ScaffoldOptions $ options ) {
76
108
$ destination_path = $ destination ->fullPath ();
77
- // This is just a sanity check; the OperationFactory has in theory already
78
- // accounted for this, and will return a SkipOp with a warning message.
79
- if (!file_exists ($ destination_path ) && empty ($ this ->default )) {
80
- throw new \RuntimeException ($ destination ->getInterpolator ()->interpolate ("Cannot append/prepend because no prior package provided a scaffold file at [dest-rel-path]. " ));
81
- }
82
109
$ interpolator = $ destination ->getInterpolator ();
83
110
84
111
// Be extra-noisy of creating a new file or appending to a non-scaffold
@@ -93,33 +120,20 @@ public function process(ScaffoldFilePath $destination, IOInterface $io, Scaffold
93
120
$ io ->write ($ interpolator ->interpolate ($ message ));
94
121
}
95
122
96
- // Fetch the prepend contents, if provided.
97
- $ prepend_contents = '' ;
123
+ // Notify that we are prepending, if there is prepend data.
98
124
if (!empty ($ this ->prepend )) {
99
125
$ this ->prepend ->addInterpolationData ($ interpolator , 'prepend ' );
100
- $ prepend_contents = file_get_contents ($ this ->prepend ->fullPath ()) . "\n" ;
101
126
$ io ->write ($ interpolator ->interpolate (" - Prepend to <info>[dest-rel-path]</info> from <info>[prepend-rel-path]</info> " ));
102
127
}
103
- // Fetch the append contents , if provided .
128
+ // Notify that we are appending , if there is append data .
104
129
$ append_contents = '' ;
105
130
if (!empty ($ this ->append )) {
106
131
$ this ->append ->addInterpolationData ($ interpolator , 'append ' );
107
- $ append_contents = "\n" . file_get_contents ($ this ->append ->fullPath ());
108
132
$ io ->write ($ interpolator ->interpolate (" - Append to <info>[dest-rel-path]</info> from <info>[append-rel-path]</info> " ));
109
133
}
110
- // We typically should always have content if we get here; the
111
- // OperationFactory should create a SkipOp instead of an AppendOp if there
112
- // is no append / prepend content. The edge case is if there is content
113
- // that is all 'trim'ed away. Then we get a message that we are appending,
114
- // although nothing will in fact actually happen.
115
- if (!empty (trim ($ prepend_contents )) || !empty (trim ($ append_contents ))) {
116
- // None of our asset files are very large, so we will load each one into
117
- // memory for processing.
118
- $ original_contents = file_get_contents (file_exists ($ destination_path ) ? $ destination_path : $ this ->default ->fullPath ());
119
- // Write the appended and prepended contents back to the file.
120
- $ altered_contents = $ prepend_contents . $ original_contents . $ append_contents ;
121
- file_put_contents ($ destination_path , $ altered_contents );
122
- }
134
+
135
+ // Write the resulting data
136
+ file_put_contents ($ destination_path , $ this ->contents ());
123
137
124
138
// Return a ScaffoldResult with knowledge of whether this file is managed.
125
139
return new ScaffoldResult ($ destination , $ this ->managed );
@@ -128,16 +142,17 @@ public function process(ScaffoldFilePath $destination, IOInterface $io, Scaffold
128
142
/**
129
143
* {@inheritdoc}
130
144
*/
131
- public function combineWithConjunctionTarget (OperationInterface $ conjunction_target ) {
132
- return new ConjunctionOp ($ conjunction_target , $ this );
145
+ public function scaffoldOverExistingTarget (OperationInterface $ existing_target ) {
146
+ $ this ->originalContents = $ existing_target ->contents ();
147
+ return $ this ;
133
148
}
134
149
135
150
/**
136
151
* {@inheritdoc}
137
152
*/
138
- public function missingConjunctionTarget (ScaffoldFilePath $ destination ) {
139
- // If there is no conjunction target (the destination is not scaffolded),
140
- // then any append we do will be to an unmanaged file.
153
+ public function scaffoldAtNewLocation (ScaffoldFilePath $ destination ) {
154
+ // If there is no existing scaffold file at the target location, then any
155
+ // append we do will be to an unmanaged file.
141
156
$ this ->managed = FALSE ;
142
157
143
158
// Default: do not allow an append over a file that was not scaffolded.
@@ -164,6 +179,9 @@ public function missingConjunctionTarget(ScaffoldFilePath $destination) {
164
179
return new SkipOp ($ message );
165
180
}
166
181
182
+ // Cache the original data to use during append.
183
+ $ this ->originalContents = $ existingData ;
184
+
167
185
return $ this ;
168
186
}
169
187
0 commit comments