CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • AssetsTask
  • CommandTask
  • ExtractTask
  • LoadTask
  • UnloadTask
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Shell\Task;
 16: 
 17: use Cake\Console\Shell;
 18: use Cake\Core\Plugin;
 19: use Cake\Filesystem\Folder;
 20: use Cake\Utility\Inflector;
 21: 
 22: /**
 23:  * Task for symlinking / copying plugin assets to app's webroot.
 24:  */
 25: class AssetsTask extends Shell
 26: {
 27:     /**
 28:      * Attempt to symlink plugin assets to app's webroot. If symlinking fails it
 29:      * fallbacks to copying the assets. For vendor namespaced plugin, parent folder
 30:      * for vendor name are created if required.
 31:      *
 32:      * @param string|null $name Name of plugin for which to symlink assets.
 33:      *   If null all plugins will be processed.
 34:      * @return void
 35:      */
 36:     public function symlink($name = null)
 37:     {
 38:         $this->_process($this->_list($name));
 39:     }
 40: 
 41:     /**
 42:      * Copying plugin assets to app's webroot. For vendor namespaced plugin,
 43:      * parent folder for vendor name are created if required.
 44:      *
 45:      * @param string|null $name Name of plugin for which to symlink assets.
 46:      *   If null all plugins will be processed.
 47:      * @return void
 48:      */
 49:     public function copy($name = null)
 50:     {
 51:         $this->_process($this->_list($name), true, $this->param('overwrite'));
 52:     }
 53: 
 54:     /**
 55:      * Remove plugin assets from app's webroot.
 56:      *
 57:      * @param string|null $name Name of plugin for which to remove assets.
 58:      *   If null all plugins will be processed.
 59:      * @return void
 60:      * @since 3.5.12
 61:      */
 62:     public function remove($name = null)
 63:     {
 64:         $plugins = $this->_list($name);
 65: 
 66:         foreach ($plugins as $plugin => $config) {
 67:             $this->out();
 68:             $this->out('For plugin: ' . $plugin);
 69:             $this->hr();
 70: 
 71:             $this->_remove($config);
 72:         }
 73: 
 74:         $this->out();
 75:         $this->out('Done');
 76:     }
 77: 
 78:     /**
 79:      * Get list of plugins to process. Plugins without a webroot directory are skipped.
 80:      *
 81:      * @param string|null $name Name of plugin for which to symlink assets.
 82:      *   If null all plugins will be processed.
 83:      * @return array List of plugins with meta data.
 84:      */
 85:     protected function _list($name = null)
 86:     {
 87:         if ($name === null) {
 88:             $pluginsList = Plugin::loaded();
 89:         } else {
 90:             if (!Plugin::isLoaded($name)) {
 91:                 $this->err(sprintf('Plugin %s is not loaded.', $name));
 92: 
 93:                 return [];
 94:             }
 95:             $pluginsList = [$name];
 96:         }
 97: 
 98:         $plugins = [];
 99: 
100:         foreach ($pluginsList as $plugin) {
101:             $path = Plugin::path($plugin) . 'webroot';
102:             if (!is_dir($path)) {
103:                 $this->verbose('', 1);
104:                 $this->verbose(
105:                     sprintf('Skipping plugin %s. It does not have webroot folder.', $plugin),
106:                     2
107:                 );
108:                 continue;
109:             }
110: 
111:             $link = Inflector::underscore($plugin);
112:             $dir = WWW_ROOT;
113:             $namespaced = false;
114:             if (strpos($link, '/') !== false) {
115:                 $namespaced = true;
116:                 $parts = explode('/', $link);
117:                 $link = array_pop($parts);
118:                 $dir = WWW_ROOT . implode(DIRECTORY_SEPARATOR, $parts) . DIRECTORY_SEPARATOR;
119:             }
120: 
121:             $plugins[$plugin] = [
122:                 'srcPath' => Plugin::path($plugin) . 'webroot',
123:                 'destDir' => $dir,
124:                 'link' => $link,
125:                 'namespaced' => $namespaced
126:             ];
127:         }
128: 
129:         return $plugins;
130:     }
131: 
132:     /**
133:      * Process plugins
134:      *
135:      * @param array $plugins List of plugins to process
136:      * @param bool $copy Force copy mode. Default false.
137:      * @param bool $overwrite Overwrite existing files.
138:      * @return void
139:      */
140:     protected function _process($plugins, $copy = false, $overwrite = false)
141:     {
142:         $overwrite = (bool)$this->param('overwrite');
143: 
144:         foreach ($plugins as $plugin => $config) {
145:             $this->out();
146:             $this->out('For plugin: ' . $plugin);
147:             $this->hr();
148: 
149:             if ($config['namespaced'] &&
150:                 !is_dir($config['destDir']) &&
151:                 !$this->_createDirectory($config['destDir'])
152:             ) {
153:                 continue;
154:             }
155: 
156:             $dest = $config['destDir'] . $config['link'];
157: 
158:             if (file_exists($dest)) {
159:                 if ($overwrite && !$this->_remove($config)) {
160:                     continue;
161:                 } elseif (!$overwrite) {
162:                     $this->verbose(
163:                         $dest . ' already exists',
164:                         1
165:                     );
166: 
167:                     continue;
168:                 }
169:             }
170: 
171:             if (!$copy) {
172:                 $result = $this->_createSymlink(
173:                     $config['srcPath'],
174:                     $dest
175:                 );
176:                 if ($result) {
177:                     continue;
178:                 }
179:             }
180: 
181:             $this->_copyDirectory(
182:                 $config['srcPath'],
183:                 $dest
184:             );
185:         }
186: 
187:         $this->out();
188:         $this->out('Done');
189:     }
190: 
191:     /**
192:      * Remove folder/symlink.
193:      *
194:      * @param array $config Plugin config.
195:      * @return bool
196:      */
197:     protected function _remove($config)
198:     {
199:         if ($config['namespaced'] && !is_dir($config['destDir'])) {
200:             $this->verbose(
201:                 $config['destDir'] . $config['link'] . ' does not exist',
202:                 1
203:             );
204: 
205:             return false;
206:         }
207: 
208:         $dest = $config['destDir'] . $config['link'];
209: 
210:         if (!file_exists($dest)) {
211:             $this->verbose(
212:                 $dest . ' does not exist',
213:                 1
214:             );
215: 
216:             return false;
217:         }
218: 
219:         if (is_link($dest)) {
220:             // @codingStandardsIgnoreLine
221:             if (@unlink($dest)) {
222:                 $this->out('Unlinked ' . $dest);
223: 
224:                 return true;
225:             } else {
226:                 $this->err('Failed to unlink  ' . $dest);
227: 
228:                 return false;
229:             }
230:         }
231: 
232:         $folder = new Folder($dest);
233:         if ($folder->delete()) {
234:             $this->out('Deleted ' . $dest);
235: 
236:             return true;
237:         } else {
238:             $this->err('Failed to delete ' . $dest);
239: 
240:             return false;
241:         }
242:     }
243: 
244:     /**
245:      * Create directory
246:      *
247:      * @param string $dir Directory name
248:      * @return bool
249:      */
250:     protected function _createDirectory($dir)
251:     {
252:         $old = umask(0);
253:         // @codingStandardsIgnoreStart
254:         $result = @mkdir($dir, 0755, true);
255:         // @codingStandardsIgnoreEnd
256:         umask($old);
257: 
258:         if ($result) {
259:             $this->out('Created directory ' . $dir);
260: 
261:             return true;
262:         }
263: 
264:         $this->err('Failed creating directory ' . $dir);
265: 
266:         return false;
267:     }
268: 
269:     /**
270:      * Create symlink
271:      *
272:      * @param string $target Target directory
273:      * @param string $link Link name
274:      * @return bool
275:      */
276:     protected function _createSymlink($target, $link)
277:     {
278:         // @codingStandardsIgnoreStart
279:         $result = @symlink($target, $link);
280:         // @codingStandardsIgnoreEnd
281: 
282:         if ($result) {
283:             $this->out('Created symlink ' . $link);
284: 
285:             return true;
286:         }
287: 
288:         return false;
289:     }
290: 
291:     /**
292:      * Copy directory
293:      *
294:      * @param string $source Source directory
295:      * @param string $destination Destination directory
296:      * @return bool
297:      */
298:     protected function _copyDirectory($source, $destination)
299:     {
300:         $folder = new Folder($source);
301:         if ($folder->copy(['to' => $destination])) {
302:             $this->out('Copied assets to directory ' . $destination);
303: 
304:             return true;
305:         }
306: 
307:         $this->err('Error copying assets to directory ' . $destination);
308: 
309:         return false;
310:     }
311: 
312:     /**
313:      * Gets the option parser instance and configures it.
314:      *
315:      * @return \Cake\Console\ConsoleOptionParser
316:      */
317:     public function getOptionParser()
318:     {
319:         $parser = parent::getOptionParser();
320: 
321:         $parser->addSubcommand('symlink', [
322:             'help' => 'Symlink (copy as fallback) plugin assets to app\'s webroot.'
323:         ])->addSubcommand('copy', [
324:             'help' => 'Copy plugin assets to app\'s webroot.'
325:         ])->addSubcommand('remove', [
326:             'help' => 'Remove plugin assets from app\'s webroot.'
327:         ])->addArgument('name', [
328:             'help' => 'A specific plugin you want to symlink assets for.',
329:             'optional' => true,
330:         ])->addOption('overwrite', [
331:             'help' => 'Overwrite existing symlink / folder / files.',
332:             'default' => false,
333:             'boolean' => true
334:         ]);
335: 
336:         return $parser;
337:     }
338: }
339: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs