问题
I'm working on a an package and I really need to be able to fire the
php artisan asset:publish --bench="vendor/package"
command automatically during development.
It's very time consuming to write that command every time I do changes to my JavaScript or CSS files in my packages.
I've tried to call Artisan in my service provider
public function boot()
{
Artisan::call('asset:publish', array('--bench' => 'arni-gudjonsson/webber'));
...
}
i got
ErrorException: Runtime Notice: Non-static method Illuminate\Foundation\Artisan::call() should not be called statically, assuming $this from incompatible context
Is Artisan not designed to be called via the web? Does anybody have some advice?
回答1:
You can use Guard for tasks like this. For example, here is a portion from my Guardfile to automatically publish assets from a package whenever they are changed:
guard :shell do
watch(%r{^workbench/vendor/package/public/.+\..+$}) {
`php artisan asset:publish --bench="vendor/package"`
}
end
You can also have it automatically compile Sass, setup livereload, etc. Take a look at Jeffrey Way's screencast to get started.
回答2:
You can achieve this using Grunt with a shell watch command, e.g:
$ npm install grunt-shell
In vendor/yourname/yourpackage/Gruntfile.js:
shell: {
assets: {
options: {
stdout: true
},
command: 'cd ../../..; php artisan asset:publish yourname/yourpackage'
},
views: {
options: {
stdout: true
},
command: 'cd ../../..; php artisan view:publish yourname/yourpackage;'
}
},
watch: {
assets: {
files: ['./public/**/*.js', './public/**/*.css'],
tasks: ['shell:assets']
},
views: {
files: ['./src/views/**/*.php', './src/views/**/*.md'],
tasks: ['shell:views']
}
}
...
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['watch']);
Then start the watch:
$ grunt watch
This can naturally work after other grunt commands such as less or uglify has compiled your assets into public, thus changing them and triggering the publish.
回答3:
This is what I'm doing:
<?php namespace Vendor\Package;
use \App;
use Illuminate\Filesystem\Filesystem;
class DecoyServiceProvider extends ServiceProvider {
public function boot() {
$this->package('bkwld/decoy');
// Auto-publish the assets when developing locally
if (App::environment() == 'local' && !App::runningInConsole()) {
$workbench = realpath(base_path().'/workbench');
if (strpos(__FILE__, $workbench) === false) App::make('asset.publisher')->publishPackage('vendor/package');
else App::make('asset.publisher')->publishPackage('vendor/package', $workbench);
}
}
}
This publishes the assets automatically whether the bundle is in the workbench or not, as long as the request is from the local environment.
回答4:
Edit /workbench/yourappspace/yourapp/AppServiceProvider.php
<?php namespace YourAppSpace\YourApp;
use Illuminate\Support\ServiceProvider;
use Artisan;
In the boot() method,
public function boot()
{
$this->package('yourappspace/yourapp');
$app = $this->app;
include __DIR__.'/../../routes.php';
Artisan::call('asset:publish', array('--bench' => 'yourappspace/yourapp'));
}
Now every time you update your code and refresh your browser, your assets are published with Artisan.
回答5:
There is an other option, that you add a composer command. For exapmple, when deploying a project to a server with install:
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize",
"php artisan asset:publish vendor-name/package-name"
],
回答6:
BTW in 5.1 right way:
\Artisan::call('vendor:publish', [
'--tag' => ['public'],
'--provider' => 'Some\Your\ServiceProvider',
'--force' => true
]);
回答7:
Try directly editing the css and js files in the
{app_root}/public/bundles/{bundle}/{css|js}
directory and after you're complete, just copy the modified files back to the bundle's public directory. This is probably the wrong way to do it, but hey, it works. Another option is to create a task and run a
Command::run(array('task_name'));
There are a few examples in the official documentation.
来源:https://stackoverflow.com/questions/14643852/automatically-call-assetpublish-bench-vendor-package-during-development-in