SharedMapController.php

This handles the storage and retrieval of shared map data in JSON format

storeSharedMap

public function storeSharedMap(Request $request)
{
    // Get JSON data from the request input.
    $jsonData = $request->input('jsonData');
    // Decode JSON data into an associative array.
    $jsonData = json_decode($jsonData, true);
    // Generate a unique hash for the filename using md5 and uniqid.
    $uniqueHash = md5(uniqid(rand(), true));
    // Define the directory where shared maps will be stored.
    $directory = 'SharedMap';
    // Create the full path for the new JSON file.
    $fileSharedMap = $directory . '/' . $uniqueHash . '.json';
    // Create the directory if it does not exist.
    if (!file_exists($directory)) {
        mkdir($directory, 0777, true);
    }
    // Convert the JSON data back to pretty-printed JSON.
    $prettyPrintedJson = json_encode($jsonData, JSON_PRETTY_PRINT);
    // Open the file for writing.
    $file = fopen($fileSharedMap, 'w');
    // If the file is successfully opened, write the JSON data and close the file.
    if ($file) {
        fwrite($file, $prettyPrintedJson);
        fclose($file);
    }
    // Prepare and return a success response with the generated filename.
    $response = [
        'SUCCESS' => 1,
        'FILENAME' => $uniqueHash,
    ];
    return $response;
}

To explain what this storeSharedMap method does:

  1. Request Handling:

    • It takes a Request object as a parameter, presumably containing JSON data for a shared map.

  2. JSON Data Handling:

    • It retrieves JSON data from the request input.

    • It decodes the JSON data into an associative array.

  3. File and Directory Handling:

    • It generates a unique hash for the filename using md5(uniqid(rand(), true)).

    • It defines the directory where shared maps will be stored as 'SharedMap'.

    • It creates the full path for the new JSON file.

  4. Directory Creation:

    • It creates the 'SharedMap' directory if it does not exist.

  5. JSON File Creation:

    • It converts the JSON data back to pretty-printed JSON.

    • It opens the file for writing and writes the JSON data.

    • If successful, it closes the file.

  6. Response Preparation:

    • It prepares a success response with the generated filename.

    • The response includes 'SUCCESS' set to 1 and 'FILENAME' set to the unique hash.

  7. Response Return:

    • It returns the prepared success response.

getSharedMap

public function getSharedMap(Request $request)
{
    // Get the filename from the request input.
    $fileName = $request->input('fileName');
    // Define the directory where shared maps are stored.
    $directory = 'SharedMap';
    // Create the full path for the JSON file.
    $fileSharedMap = $directory . '/' . $fileName . '.json';
    // Check if the file exists.
    if (file_exists($fileSharedMap)) {
        // Read the content of the file.
        $fileContent = file_get_contents($fileSharedMap);
        // If file content is successfully retrieved, decode it into an associative array.
        if ($fileContent !== false) {
            // Prepare and return a success response with the retrieved data.
            $response = [
                'SUCCESS' => 1,
                'DATA' => json_decode($fileContent, true),
            ];
            return $response;
        }
    } else {
        // If the file does not exist, prepare and return a failure response.
        $response = [
            'SUCCESS' => 0,
        ];
        return $response;
    }
}

To explain what this getSharedMap method does:

  1. Request Handling:

    • It takes a Request object as a parameter, presumably containing the filename of the shared map to retrieve.

  2. Filename Extraction:

    • It extracts the filename from the request input.

  3. File and Directory Handling:

    • It defines the directory where shared maps are stored as 'SharedMap'.

    • It creates the full path for the JSON file.

  4. File Existence Check:

    • It checks if the file with the specified filename exists.

  5. File Reading and Decoding:

    • If the file exists, it reads the content of the file.

    • It decodes the file content into an associative array.

  6. Response Preparation (Success):

    • If successful, it prepares a success response with the retrieved data.

    • The response includes 'SUCCESS' set to 1 and 'DATA' containing the decoded file content.

  7. Response Preparation (Failure):

    • If the file does not exist, it prepares a failure response with 'SUCCESS' set to 0.

  8. Response Return:

    • It returns the prepared success or failure response.

Last updated