11<?php
22
3- namespace PlacesAutocomplete \View \Components ;
4-
5- use Illuminate \View \Component ;
6- use \Illuminate \Support \Str ;
3+ namespace PlacesAutocomplete ;
74
5+ use Illuminate \Support \Facades \Blade ;
6+ use Illuminate \Support \ServiceProvider ;
7+ use PlacesAutocomplete \View \Components \PlacesAutocompleteComponent ;
88
99/**
10- * Class PlacesAutocompleteComponent
11- *
12- * A Blade component for integrating Google Places Autocomplete.
13- *
14- * @package PlacesAutocomplete\View\Components
10+ * Service provider for the Places Autocomplete package.
1511 */
16-
17- class PlacesAutocompleteComponent extends Component
12+ class PlacesAutocompleteServiceProvider extends ServiceProvider
1813{
19- public string $ containerId ; // Unique ID for the container element
20- public string $ rootDivId ; // ID for the root div element
21- public string $ googleMapsApiKey ; // Google Maps API key for Places Autocomplete
22- public array $ options ; // Options for the Places Autocomplete component
23- public array $ requestParams ; // Request parameters for the Places Autocomplete component
24- public array $ fetchFields ; // Fetch fields for the Places Autocomplete component
25- public bool $ loadScriptTag ; // New option to control script loading
26-
27-
2814 /**
29- * Class constructor.
30- *
31- * @param string $containerId
32- * @param string $rootDivId
33- * @param string $googleMapsApiKey
34- * @param array $options
35- * @param array $requestParams
36- * @param array $fetchFields
37- * @param bool $loadScriptTag
15+ * Register any application services.
3816 */
39- public function __construct (
40- string $ containerId = 'pac_container ' ,
41- string $ rootDivId = 'places-autocomplete-root ' ,
42- string $ googleMapsApiKey = '' ,
43- array $ options = [],
44- array $ requestParams = [],
45- array $ fetchFields = [],
46- bool $ loadScriptTag = true // Default to true
47- ) {
48-
49- // Ensure unique ID
50- $ this ->containerId = $ containerId . '- ' . Str::random (5 );
51-
52- $ this ->rootDivId = $ rootDivId ;
53- // Set Google Maps API key, options, request parameters, fetch fields, and script loading option
54- $ this ->googleMapsApiKey = !empty ($ googleMapsApiKey ) ? $ googleMapsApiKey : config ('places_autocomplete.google_maps_api_key ' );
55- // Merge with default options, request parameters, and fetch fields from config
56- $ this ->options = array_merge (config ('places_autocomplete.options ' , []), $ options );
57-
58- // Request parameters and fetch fields are merged with defaults from config
59- // This allows users to override them when using the component in their Blade templates
60- $ this ->requestParams = array_merge (config ('places_autocomplete.request_params ' , []), $ requestParams );
61-
62- // Fetch fields are merged with defaults from config
63- // This allows users to override them when using the component in their Blade templates
64- // Default fetch fields include 'formattedAddress' and 'addressComponents'
65- $ this ->fetchFields = array_merge (config ('places_autocomplete.fetch_fields ' , ['formattedAddress ' , 'addressComponents ' ]), $ fetchFields );
66-
67- // Set the loadScriptTag property to control whether the script tag is loaded
68- // This allows users to choose whether to load the Google Maps script tag automatically
69- $ this ->loadScriptTag = $ loadScriptTag ;
70-
71- // Validate that the Google Maps API key is set
72- // If not set, throw an exception to inform the user
73- if (empty ($ this ->googleMapsApiKey )) {
74- throw new \InvalidArgumentException ('Google Maps API key is not configured. Please set it in your .env file or config/places_autocomplete.php ' );
75- }
76- }
17+ public function register (): void {}
7718
7819 /**
79- * Render the component view.
80- *
81- * @return void
20+ * Bootstrap any application services.
8221 */
83- public function render ()
22+ public function boot (): void
8423 {
85- return view ('vendor.places-autocomplete.places-autocomplete ' , [
86- 'containerId ' => $ this ->containerId ,
87- 'rootDivId ' => $ this ->rootDivId ,
88- 'googleMapsApiKey ' => $ this ->googleMapsApiKey ,
89- 'options ' => $ this ->options ,
90- 'requestParams ' => $ this ->requestParams ,
91- 'fetchFields ' => $ this ->fetchFields ,
92- 'loadScriptTag ' => $ this ->loadScriptTag ,
24+ // Publishing the configuration file
25+ $ this ->publishes ([
26+ __DIR__ . '/config/places_autocomplete.php ' => config_path ('places_autocomplete.php ' ),
27+ ], 'places-autocomplete ' );
28+
29+ // Publishing the JavaScript assets
30+ $ this ->publishes ([
31+ __DIR__ . '/js ' => public_path ('vendor/places-autocomplete ' ),
32+ ], 'places-autocomplete ' );
33+
34+ // Publishing the view files
35+ $ this ->publishes ([
36+ __DIR__ . '/views ' => resource_path ('views/vendor/places-autocomplete ' ),
9337 ]);
38+
39+ // Registering the Blade component
40+ // This allows the component to be used in Blade templates
41+ // The component can be used with the syntax: <x-places-autocomplete />
42+ // The component will be rendered using the PlacesAutocompleteComponent class
43+ Blade::component ('places-autocomplete ' , PlacesAutocompleteComponent::class);
9444 }
95- }
96- // End of PlacesAutocompleteComponent class
45+ }
0 commit comments