function CreatePackageJson()
 {
     $extensionData = ExtensionData::get();
     $count = 0;
     if ($extensionData && !empty($extensionData)) {
         $count = $extensionData->Count();
         $filename = 'packages.json';
         $repo = array('packages' => array());
         foreach ($extensionData as $extension) {
             // Include only Approved extensions
             if ($extension->Accepted == '1') {
                 $json = new JsonHandler($extension->Url);
                 $jsonData = $json->cloneJson();
                 $packages = $jsonData['AllRelease'];
                 $dumper = new ArrayDumper();
                 foreach ($packages as $package) {
                     $repo['packages'][$package->getPrettyName()][$package->getPrettyVersion()] = $dumper->dump($package);
                 }
             }
         }
         if (!empty($repo['packages'])) {
             $packagesJsonData = Convert::array2json($repo);
             $packageJsonFile = fopen(BASE_PATH . DIRECTORY_SEPARATOR . $filename, 'w');
             fwrite($packageJsonFile, $packagesJsonData);
             fclose($packageJsonFile);
             echo "<br /><br /><strong> package.json file created successfully...</strong><br />";
         } else {
             throw new InvalidArgumentException('package.json file could not be created');
         }
     }
 }
 function updateJson()
 {
     $extensionData = ExtensionData::get();
     $count = 0;
     if ($extensionData && !empty($extensionData)) {
         $count = 0;
         foreach ($extensionData as $extension) {
             // Include only Approved extensions
             if ($extension->Accepted == '1') {
                 $json = new JsonHandler($extension->Url);
                 $jsonData = $json->cloneJson();
                 $updateJson = $json->UpdateJson();
                 if (array_key_exists('ExtensionID', $updateJson)) {
                     $id = $updateJson['ExtensionID'];
                     $deleteVersion = $json->deleteVersionData($id);
                     if ($deleteVersion) {
                         $saveVersion = $json->saveVersionData($id);
                         if ($saveVersion) {
                             echo "<br>{$extension->Name} is updated <br>";
                         }
                     }
                 } else {
                     throw new InvalidArgumentException("{$extension->Name}  could not updated <br />");
                 }
                 $count++;
             }
         }
         echo "<br><br><strong>{$count} Repositories processed...</strong><br>";
     } else {
         throw new InvalidArgumentException('No Extension found...');
     }
 }
 function testSaveJsonFunction()
 {
     $repoUrl = 'https://github.com/vikas-srivastava/extensionmanager.git';
     $jsonHandler = new JsonHandler($repoUrl);
     $jsonHandler->repo = new MockVCSRepositoryWithRealValues();
     $result = $jsonHandler->cloneJson();
     $this->latestReleasePackage = $result['LatestRelease'];
     $result = $jsonHandler->saveJson();
     $this->assertArrayNotHasKey('ErrorMsg', $result);
     $this->assertArrayHasKey('ExtensionID', $result);
     $extensionId = $result['ExtensionID'];
     $extensionData = ExtensionData::get()->byID($extensionId);
     list($vendorName, $extensionName) = explode("/", $this->latestReleasePackage->getPrettyName());
     $this->assertEquals($extensionData->Name, $extensionName);
 }
 /**
  * The form handler.
  */
 public function submitUrl($data, $form)
 {
     $url = $data['Url'];
     if (empty($url)) {
         $form->sessionMessage(_t('ExtensionHolderPage.BADURL', 'Please enter URL'), 'bad');
         return $this->redirectBack();
     } elseif (substr($url, 0, 4) != "http" && substr($url, 0, 3) != "git") {
         $form->sessionMessage(_t('ExtensionHolderPage.BADURL', "Please enter valid 'HTTP' or 'GIT Read-only' URL "), 'bad');
         return $this->redirectBack();
     } elseif (substr($url, 0, 4) == "git@") {
         $form->sessionMessage(_t('ExtensionHolderPage.BADURL', "'SSH' URL is not allowed, Please enter valid 'HTTP' or 'GIT Read-only' URL"), 'bad');
         return $this->redirectBack();
     }
     $json = new JsonHandler($url);
     $jsonData = $json->cloneJson();
     if (!array_key_exists('ErrorMsg', $jsonData)) {
         $this->latestReleasePackage = $jsonData['LatestRelease'];
         if ($this->isNewExtension($url)) {
             $saveJson = $json->saveJson();
             if (!array_key_exists('ErrorMsg', $saveJson)) {
                 $id = $saveJson['ExtensionID'];
                 $saveVersion = $json->saveVersionData($id);
                 if ($saveVersion) {
                     $this->extensionType = ExtensionData::get()->byID($id)->Type;
                     $this->extensionName = ExtensionData::get()->byID($id)->Name;
                     $this->mailData = array('ExtensionType' => $this->extensionType, 'ExtensionName' => $this->extensionName, 'SubmittedByName' => Member::currentUser()->Name, 'SubmittedByEmail' => Member::currentUser()->Email, 'ReviewAtUrl' => Director::absoluteBaseURL() . 'admin/extensions/ExtensionData/EditForm/field/ExtensionData/item/' . $id . '/edit', 'DetailPageLink' => Director::absoluteBaseURL() . strtolower($this->extensionType) . '/show/' . $id, 'Subject' => 'New ' . $this->extensionType . " '" . $this->extensionName . "' " . ' Submitted');
                     $this->sendMailtoAdmin();
                     $form->sessionMessage(_t('ExtensionHolderPage.THANKSFORSUBMITTING', 'Thank you for submitting "' . $this->extensionName . '" ' . $this->extensionType), 'good');
                     return $this->redirectBack();
                 }
             } else {
                 $form->sessionMessage(_t('ExtensionHolderPage.PROBLEMINSAVING', "We are unable to save Extension info, the parser reports: {$saveJson['ErrorMsg']} Please Re-check format of you composer.json file."), 'bad');
                 return $this->redirectBack();
             }
         } else {
             $updateJson = $json->updateJson();
             if (!array_key_exists('ErrorMsg', $updateJson)) {
                 $id = $updateJson['ExtensionID'];
                 $deleteVersion = $json->deleteVersionData($id);
                 if ($deleteVersion) {
                     $saveVersion = $json->saveVersionData($id);
                     if ($saveVersion) {
                         $this->extensionType = ExtensionData::get()->byID($id)->Type;
                         $this->extensionName = ExtensionData::get()->byID($id)->Name;
                         $this->mailData = array('ExtensionType' => $this->extensionType, 'ExtensionName' => $this->extensionName, 'SubmittedByName' => Member::currentUser()->Name, 'SubmittedByEmail' => Member::currentUser()->Email, 'ReviewAtUrl' => Director::absoluteBaseURL() . 'admin/extensions/ExtensionData/EditForm/field/ExtensionData/item/' . $id . '/edit', 'DetailPageLink' => Director::absoluteBaseURL() . strtolower($this->extensionType) . '/show/' . $id, 'Subject' => $this->extensionType . " '" . $this->extensionName . "' " . ' Updated');
                         $this->sendMailtoAdmin();
                         $form->sessionMessage(_t('ExtensionHolderPageExtensionHolderPage.THANKSFORUPDATING', 'Thank you for Updating your "' . $this->extensionName . '" ' . $this->extensionType), 'good');
                         return $this->redirectBack();
                     }
                 } else {
                     $form->sessionMessage(_t('ExtensionHolderPage.PROBLEMINSAVING', 'Something went wrong we are not able to save versions of submitted extension '), 'bad');
                     return $this->redirectBack();
                 }
             } else {
                 $form->sessionMessage(_t('ExtensionHolderPage.PROBLEMINUPDATING', "We had problems parsing your composer.json file, the parser reports: {$updateJson['ErrorMsg']} Please read our extension Submission Guide for more details and submit url again"), 'bad');
                 return $this->redirectBack();
             }
         }
     } else {
         $form->sessionMessage(_t('ExtensionHolderPage.NOJSON', "We had problems parsing your composer.json file, the parser reports: {$jsonData['ErrorMsg']} Please read our extension Submission Guide for more details and submit url again"), 'bad');
         return $this->redirectBack();
     }
 }