|
| 1 | +--- |
| 2 | +title: System |
| 3 | +order: 800 |
| 4 | +--- |
| 5 | +# The System |
| 6 | + |
| 7 | +One of the main advantages of building a native application is having more direct access to system resources, such as |
| 8 | +peripherals connected to the physical device and APIs that aren't typically accessible inside a browser's sandbox. |
| 9 | + |
| 10 | +NativePHP makes it trivial to access these resources and APIs. |
| 11 | + |
| 12 | +One of the main challenges - particularly when writing cross-platform apps - is that each operating system has |
| 13 | +its own set of available APIs, along with their own idiosyncrasies. |
| 14 | + |
| 15 | +NativePHP smooths over as much of this as possible, to offer a simple and consistent set of interfaces regardless of |
| 16 | +the platform on which your app is running. |
| 17 | + |
| 18 | +While some features are platform-specific, NativePHP gracefully handles this for you so that you don't have to think |
| 19 | +about whether something is Linux-, Mac-, or Windows-only. |
| 20 | + |
| 21 | +## Prompt for TouchID |
| 22 | + |
| 23 | +For Mac systems that support TouchID, you can use TouchID to protect and unlock various parts of your application: |
| 24 | + |
| 25 | +```php |
| 26 | +use Native\Laravel\Facades\System; |
| 27 | + |
| 28 | +if (System::canPromptTouchID() && System::promptTouchID()) { |
| 29 | + // Do you super secret activity here |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Printing |
| 34 | + |
| 35 | +You can list all available printers: |
| 36 | + |
| 37 | +```php |
| 38 | +@use(Native\Laravel\Facades\System) |
| 39 | + |
| 40 | +@foreach(System::printers() as $printer) |
| 41 | + {{ $printer->displayName }} |
| 42 | +@foreach |
| 43 | +``` |
| 44 | + |
| 45 | +Each item in the printers array is a `\Native\Laravel\DataObjects\Printer` which contains various device details and |
| 46 | +default configuration. |
| 47 | + |
| 48 | +You can send some HTML to be printed like this: |
| 49 | + |
| 50 | +```php |
| 51 | +System::print('<html>...', $printer); |
| 52 | +``` |
| 53 | + |
| 54 | +If no `$printer` object is provided, the default printer and settings will be used. |
| 55 | + |
| 56 | +You can change the configuration before sending something to be printed, for example if you want multiple copies: |
| 57 | + |
| 58 | +```php |
| 59 | +$printer->options['copies'] = 5; |
| 60 | + |
| 61 | +System::print('<html>...', $printer); |
| 62 | +``` |
| 63 | + |
| 64 | +You can also print directly to PDF: |
| 65 | + |
| 66 | +```php |
| 67 | +System::printToPDF('<html>...'); |
| 68 | +``` |
| 69 | + |
| 70 | +This returns the PDF data in a `base64_encoded` binary string. So be sure to `base64_decode` it before storing it to disk. |
| 71 | +You could save it directly to disk or prompt the user for a location to save it: |
| 72 | + |
| 73 | +```php |
| 74 | +use Illuminate\Support\Facades\Storage; |
| 75 | + |
| 76 | +$pdf = System::printToPDF('<html>...'); |
| 77 | + |
| 78 | +Storage::disk('desktop')->put('My Awesome File.pdf', base64_decode($pdf)); |
| 79 | +``` |
| 80 | + |
| 81 | +## Time Zones |
| 82 | + |
| 83 | +PHP and your Laravel application will generally be configured to work with a specific time zone. This could be UTC, for |
| 84 | +example. |
| 85 | + |
| 86 | +But users of your application will think about time differently. Normally, the user's perspective of time is reflected |
| 87 | +in their operating system's time zone setting. |
| 88 | + |
| 89 | +NativePHP includes a mechanism to translate cross-platform time zone identifiers to consistent identifiers that PHP |
| 90 | +expects to use. |
| 91 | + |
| 92 | +You can use this to show dates and times in the appropriate time zone without having to ask your users to manually |
| 93 | +select their current time zone. |
| 94 | + |
| 95 | +**Note: It some cases, this mechanism may not select the exact time zone of the user. It uses an approximation to |
| 96 | +simplify things, as there are many overlapping time zones and methods of naming them.** |
| 97 | + |
| 98 | +Using this approach, your app will be responsive to changes in the system's time zone settings, e.g. in case the |
| 99 | +user moves between timezones. |
| 100 | + |
| 101 | +Get the current system time zone: |
| 102 | + |
| 103 | +```php |
| 104 | +$timezone = System::timezone(); |
| 105 | + |
| 106 | +// $timezone => 'Europe/London' |
| 107 | +``` |
0 commit comments