@@ -49,6 +49,13 @@ class AppendOp extends AbstractOperation {
4949 */
5050 protected $ forceAppend ;
5151
52+ /**
53+ * The contents from the file that we are prepending / appending to.
54+ *
55+ * @var string
56+ */
57+ protected $ originalContents ;
58+
5259 /**
5360 * Constructs an AppendOp.
5461 *
@@ -69,16 +76,36 @@ public function __construct(ScaffoldFilePath $prepend_path = NULL, ScaffoldFileP
6976 $ this ->managed = TRUE ;
7077 }
7178
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+
72104 /**
73105 * {@inheritdoc}
74106 */
75107 public function process (ScaffoldFilePath $ destination , IOInterface $ io , ScaffoldOptions $ options ) {
76108 $ 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- }
82109 $ interpolator = $ destination ->getInterpolator ();
83110
84111 // 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
93120 $ io ->write ($ interpolator ->interpolate ($ message ));
94121 }
95122
96- // Fetch the prepend contents, if provided.
97- $ prepend_contents = '' ;
123+ // Notify that we are prepending, if there is prepend data.
98124 if (!empty ($ this ->prepend )) {
99125 $ this ->prepend ->addInterpolationData ($ interpolator , 'prepend ' );
100- $ prepend_contents = file_get_contents ($ this ->prepend ->fullPath ()) . "\n" ;
101126 $ io ->write ($ interpolator ->interpolate (" - Prepend to <info>[dest-rel-path]</info> from <info>[prepend-rel-path]</info> " ));
102127 }
103- // Fetch the append contents , if provided .
128+ // Notify that we are appending , if there is append data .
104129 $ append_contents = '' ;
105130 if (!empty ($ this ->append )) {
106131 $ this ->append ->addInterpolationData ($ interpolator , 'append ' );
107- $ append_contents = "\n" . file_get_contents ($ this ->append ->fullPath ());
108132 $ io ->write ($ interpolator ->interpolate (" - Append to <info>[dest-rel-path]</info> from <info>[append-rel-path]</info> " ));
109133 }
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 ());
123137
124138 // Return a ScaffoldResult with knowledge of whether this file is managed.
125139 return new ScaffoldResult ($ destination , $ this ->managed );
@@ -128,16 +142,17 @@ public function process(ScaffoldFilePath $destination, IOInterface $io, Scaffold
128142 /**
129143 * {@inheritdoc}
130144 */
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 ;
133148 }
134149
135150 /**
136151 * {@inheritdoc}
137152 */
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.
141156 $ this ->managed = FALSE ;
142157
143158 // Default: do not allow an append over a file that was not scaffolded.
@@ -164,6 +179,9 @@ public function missingConjunctionTarget(ScaffoldFilePath $destination) {
164179 return new SkipOp ($ message );
165180 }
166181
182+ // Cache the original data to use during append.
183+ $ this ->originalContents = $ existingData ;
184+
167185 return $ this ;
168186 }
169187
0 commit comments