bcit-ci / CodeIgniter

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
18.26k stars 7.6k forks source link

Profiler only supports HTML output - Virtually impossible to use for JSON API and/or CLI #4477

Closed ashiina closed 2 years ago

ashiina commented 8 years ago

While the CodeIgniter profiler is useful, it is only able to output data via HTML. We use CodeIgniter for REST APIs (JSON output), and also use CLI outputs. The profiler outputs a ton of HTML tags, so it is almost impossible to use the profiler in any sort of practical way.

I propose that the profiler output should be done via a template file (like the DB errors being outputted via the errors/error_db.php template), instead of raw HTML code written in the core.

If this is a valid proposal, I would be more than happy to work on this myself and submit a PR. I just want to know beforehand if I am not facing the wrong direction, since I will need to put some amount of work into this.

narfbg commented 8 years ago

Sure, that'd be a welcome improvement, although I can't guarantee that I'd like the patch itself.

However, please note that CI itself was never designed for and is itself not suitable for building REST APIs, and its CLI appliances are limited. Switching the profiler to use templates won't change that.

ashiina commented 8 years ago

Thanks for the reply. Yep I understand that CI is primarily for web applications so APIs/CLIs are not its priority - though I believe there should be somewhat of a workaround for people with those use-cases (without hacking into the framework's source code).

I will submit a PR once I get to this, so please tell me what you think then!

aanbar commented 8 years ago

I think this is a great idea, but I would suggest converting profiling statistics into a data structure which means _compile_* methods will append data to an array that's defined in the class then the method that does the output would use the compiled data from that array.

Keep in mind that simply returning JSON as output won't get your API data correct as you'd end up with 2 json arrays unless you have a way to merge profiling data to your original API output.

The best way I would think off is generating a JSON encoded array of the profiling data & appending it as a commented out structure so it won't affect your API output

Don't forget to add these flags when you output the JSON profiling data JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES

I would suggest modifying $this->output->enable_profiler() method after you finish so it accepts a second argument to set the output type e.g 'html', 'json'

ashiina commented 8 years ago

@aanbar Thanks for the tips! Converting the profiling statistics is exactly the approach I am taking so glad to hear I am on the same page :) As for the JSON, this proposal isn't meant to accommodate the need to support JSON output. As narfg says,

However, please note that CI itself was never designed for and is itself not suitable for building REST APIs, and its CLI appliances are limited. Switching the profiler to use templates won't change that.

I would like to comply with this general philosophy. I just wanted to simply refactor the profiler so that the profiling process and the view (currently HTML) are separated, so that anyone who doesn't like the current view can modify the template on their own to suit their needs.

MahdiMajidzadeh commented 8 years ago

I think a library can help, change profiler's output to json

danielrhodeswarp commented 7 years ago

From my perspective, it would be very useful - and actually feels like a missed trick - to have an option or setting for the profiler to output plaintext. This is especially useful considering that CI controllers are all basically available on the CLI. Not too sure I'd fiddle about with templates personally but an option for plaintext would be cool.

pgee70 commented 6 years ago

This isn't a solution, but is a description of my work-around. I am very comfortable with CI, so i use it for RESTful services.
In the REST controller i changed the code to write out the string of the profiler HTML as a JSON node = profile (only if in development environment) Then on the REACT boilerplate front end client when a server-response is returned, i use a reducer that puts the profile HTML if present into the redux state. Then a 'Selector' grabs the data from the redux state which is rendered to a button/dialog to view the code. I use the the REACT dangerously set the profile HTML using React. works great.

narfbg commented 2 years ago

No use having this issue open.