BEAR.Ace is an Ace utility for PHP. (Ace is a standalone code editor written in JavaScript.) It enables you to use an editor via a web service or to fix syntax errors on the fly.
BEAR.AceはオンラインエディターAceのユーティリティです。 エディターwebサービスやシンタックスエラーのオンライン修正が可能です。
The recommended way to install BEAR.Ace is through Composer.
# Install Composer
curl -sS https://getcomposer.org/installer | php
# Install as stand alone editor
php composer.phar create-project bear/ace BEAR.Ace
# Add BEAR.Ace as a dependency
php composer.phar require bear/ace:~1.0
$ cd BEAR.Ace/web
$ php -S localhost:8090 index.php
You can now browse file content using the 'file' and 'line' (optional) query.
You can also save the file you are editing when you have write access to the web server. It supports save shortcut keys for Windows(Ctl+S) and OSX(Cmd+S).
Getting HTML to display in the editor.
$html = (string)(new Editor)->setRootPath($rootPath)->setPath($file)->setLine($line);
You can specify a $file
as an absolute path or as a relative path from $rootPath
.
Files higher than $rootPath
are not accessible for security reasons.
Starting the online service is simple using the following code.
try {
$editor = (new Editor)->setRootPath($rootPath)->handle($_GET, $_POST, $_SERVER);
echo $editor;
} catch (Exception $e) {
http_response_code($e->getCode());
echo $e->getCode();
}
More sample code can be found in the /docs/ directory.
Once an error handler has been registered, when a syntax error occurs, you can not only display the error but make a fix in the browser on the fly. The browser is then automatically reloaded for you upon save. This feature can really minimize the time and frustration caused by simple careless mistakes.
(new \BEAR\Ace\ErrorEditor)->registerSyntaxErrorEdit();
You can also view file content in the editor by using the edit function. Just specify the object or file path in the argument.
$file = __DIR__ . 'file.php';
edit($file);
$a = new A;
edit($a);
The online editor can be linked to in the stack trace file name using the following ini configuration of xdebug.
xdebug.file_link_format=localhost:8090/?file=%f&line=$l
"require": {
...
"bear/ace": "*"
},
$ composer update bear/ace
require_once __DIR__.'/../app/AppKernel.php';
// after this line
(new \BEAR\Ace\ErrorEditor)->registerSyntaxErrorEdit();
// before this line
$kernel = new AppKernel('dev', true);