2014-11-04 2 views
6

Моя проблема заключается в том, как загрузить .js-файлы из папки поставщиков в CakePHP 3.0. Я включил twitter bootstrap через композитор. Файл .js находится в папке/vendor/twbs/bootstrap-sass/assets/javascripts /. Я не хочу переместить его на webroot, потому что тогда я сломаю функции автоматического обновления, предоставляемые композитором. Любые хорошие предложения? Я не хочу дублировать файлы и освобождать преимущества композитора ...Загрузка файлов javascript от поставщиков в CakePHP 3

ответ

6

Я нашел решение! Если я использую композитора, почему бы не использовать его для этого тоже, верно? :)

В composer.json:

"scripts": { 
    "post-install-cmd": "App\\Console\\Installer::postInstall", 
    "post-update-cmd": "App\\Console\\Installer::postUpdate" 
} 

В ЦСИ/консоли/Installer.php:

public static function postUpdate(Event $event) { 
    $io = $event->getIO(); 

    $rootDir = dirname(dirname(__DIR__)); 

    static::copyTwitterBootstrapFiles($rootDir, $io); 
} 

public static function copyTwitterBootstrapFiles($dir, $io) { 

    $bootstrapJsSource = $dir . '/vendor/twbs/bootstrap-sass/assets/javascripts/bootstrap.js'; 
    $bootstrapJsDestination = $dir . '/webroot/js/bootstrap.js'; 

    if (file_exists($bootstrapJsSource)) { 
     copy($bootstrapJsSource, $bootstrapJsDestination); 
     $io->write('Copied `bootstrap.js` file'); 
    } 

} 

И, наконец, если вы используете Git добавить WebRoot/bootstrap.js в .gitignore , PostUpdate запускается после каждой команды обновления композитора, поэтому, если вы хотите запустить скрипт после каждого обновления фактического пакета, просто используйте post-package-update вместо post-update-cmd.

0

То же самое с ответом mutdsu, но с более подробной информацией.

В composer.json под скрипты, добавьте строку:

"post-update-cmd": "App\\Console\\Installer::postUpdate", 

он должен показать что-то вроде этого:

"scripts": { 
    ... 
    "post-update-cmd": "App\\Console\\Installer::postUpdate", 
    ... 
}, 

в ИПВ/консоли/Installer.php, добавьте эти две статические функции:

public static function postUpdate(Event $event) { 
    $io = $event->getIO(); 
    $rootDir = dirname(dirname(__DIR__)); 
    static::copyBootstrapAssets($rootDir, $io); 
} 

public static function copyBootstrapAssets($dir, $io) { 
    $ds = DIRECTORY_SEPARATOR; 
    $bootstrapAssetsDir = $dir . $ds . 'vendor' . $ds . 'twbs' . $ds . 'bootstrap' . $ds . 'dist' . $ds; 
    $bootstrapCssAssetsDir = $bootstrapAssetsDir . 'css' . $ds; 
    $bootstrapJsAssetsDir = $bootstrapAssetsDir . 'js' . $ds; 
    $bootstrapFontAssetsDir = $bootstrapAssetsDir . 'fonts' . $ds; 
    $webrootDir = $dir . $ds . 'webroot' . $ds; 
    $cssDir = $webrootDir . 'css' . $ds; 
    $jsDir = $webrootDir . 'js' . $ds; 
    $fontDir = $webrootDir . 'fonts' . $ds; 
    if (!file_exists($cssDir) && !is_dir($cssDir)) { 
     mkdir($cssDir); 
    } 
    if (!file_exists($jsDir) && !is_dir($jsDir)) { 
     mkdir($jsDir); 
    } 
    if (!file_exists($fontDir) && !is_dir($fontDir)) { 
     mkdir($fontDir); 
    } 
    $cssAssets = [ 
     'bootstrap.min.css', 
     'bootstrap-theme.min.css', 
    ]; 
    $jsAssets = [ 
     'bootstrap.min.js', 
    ]; 
    $fontAssets = [ 
     'glyphicons-halflings-regular.eot', 
     'glyphicons-halflings-regular.svg', 
     'glyphicons-halflings-regular.ttf', 
     'glyphicons-halflings-regular.woff', 
     'glyphicons-halflings-regular.woff2', 
    ]; 
    foreach ($cssAssets as $asset) { 
     if (file_exists($bootstrapCssAssetsDir . $asset)) { 
      copy($bootstrapCssAssetsDir . $asset, $cssDir . $asset); 
      $io->write('Copied `' . $asset . '` file'); 
     } else { 
      if (file_exists($cssDir . $asset)) { 
       unlink($cssDir . $asset); 
      } 
     } 
    } 
    foreach ($jsAssets as $asset) { 
     if (file_exists($bootstrapJsAssetsDir . $asset)) { 
      copy($bootstrapJsAssetsDir . $asset, $jsDir . $asset); 
      $io->write('Copied `' . $asset . '` file'); 
     } else { 
      if (file_exists($jsDir . $asset)) { 
       unlink($jsDir . $asset); 
      } 
     } 
    } 
    foreach ($fontAssets as $asset) { 
     if (file_exists($bootstrapFontAssetsDir . $asset)) { 
      copy($bootstrapFontAssetsDir . $asset, $fontDir . $asset); 
      $io->write('Copied `' . $asset . '` file'); 
     } else { 
      if (file_exists($fontDir . $asset)) { 
       unlink($fontDir . $asset); 
      } 
     } 
    } 
} 

Если вы используете Git, пожалуйста, убедитесь, чтобы добавить эти строки в файл .gitignore:

/webroot/css/bootstrap.min.css 
/webroot/css/bootstrap-theme.min.css 
/webroot/js/bootstrap.min.js 
/webroot/fonts/glyphicons-halflings-regular.eot 
/webroot/fonts/glyphicons-halflings-regular 
/webroot/fonts/glyphicons-halflings-regular.ttf 
/webroot/fonts/glyphicons-halflings-regular.woff 
/webroot/fonts/glyphicons-halflings-regular.woff2 
/webroot/fonts/glyphicons-halflings-regular.woff2