/** * @return {PjaxResponseNegotiator} * * @see LeftAndMain::getResponseNegotiator() */ public function getResponseNegotiator() { $neg = parent::getResponseNegotiator(); $controller = $this; $neg->setCallback('CurrentForm', function () use(&$controller) { return $controller->renderWith($controller->getTemplatesWithSuffix('_Content')); }); return $neg; }
/** * Adds the default languages if they are missing */ public function requireDefaultRecords() { parent::requireDefaultRecords(); $defaultLangs = array_keys($this->defaultLanguages); $dbLangCount = SnippetLanguage::get()->filter('Name', $defaultLangs)->filter('UserLanguage', 0)->Count(); if ($dbLangCount < count($defaultLangs)) { foreach ($this->defaultLanguages as $name => $data) { if (!SnippetLanguage::get()->find('Name', $name)) { $lang = new SnippetLanguage(); $lang->Name = $name; $lang->FileExtension = $data['Extension']; $lang->HighlightCode = $data['HighlightCode']; $lang->UserLanguage = false; $lang->write(); DB::alteration_message('Created snippet language "' . $name . '"', 'created'); } } } //Look for config languages $configLanguages = CodeBank::config()->extra_languages; if (!empty($configLanguages)) { foreach ($configLanguages as $language) { //Validate languages if (empty($language['Name']) || empty($language['FileName']) || empty($language['HighlightCode']) || empty($language['Brush'])) { user_error('Invalid snippet user language found in config, user languages defined in config must contain a Name, FileName, HighlightCode and Brush file path', E_USER_WARNING); continue; } $lang = SnippetLanguage::get()->filter('Name', Convert::raw2sql($language['Name']))->filter('HighlightCode', Convert::raw2sql($language['HighlightCode']))->filter('UserLanguage', true)->first(); if (empty($lang) || $lang === false || $lang->ID <= 0) { if (Director::is_absolute($language['Brush']) || Director::is_absolute_url($language['Brush'])) { user_error('Invalid snippet user language found in config, user languages defined in config must contain a path to the brush relative to the SilverStripe base (' . Director::baseFolder() . ')', E_USER_WARNING); continue; } if (preg_match('/\\.js$/', $language['Brush']) == 0) { user_error('Invalid snippet user language found in config, user languages defined in config must be javascript files', E_USER_WARNING); continue; } //Add language $lang = new SnippetLanguage(); $lang->Name = $language['Name']; $lang->FileExtension = $language['FileName']; $lang->HighlightCode = $language['HighlightCode']; $lang->BrushFile = $language['Brush']; $lang->UserLanguage = true; $lang->write(); DB::alteration_message('Created snippet user language "' . $language['Name'] . '"', 'created'); } } } }
/** * Tests to see if a language defined in the config with a brush that does not end in .js generates an error and is not added */ public function testNonJSCustomLanguageBrush() { CodeBank::config()->extra_languages = array(array('Name' => 'Test Language 4', 'FileName' => 'tst', 'HighlightCode' => 'tst4', 'Brush' => 'CodeBank/tests/files/testBrush.lorem')); $didExpectedError = false; try { //Try populating the default records singleton('SnippetLanguage')->requireDefaultRecords(); } catch (PHPUnit_Framework_Error_Warning $e) { if (strpos($e->getMessage(), 'Invalid snippet user language found in config') !== false) { $didExpectedError = true; } else { throw $e; //Ensure the error is re-thrown, it doesn't match what we're expecting } } //Check to see if the error was thrown $this->assertEquals(true, $didExpectedError); //Make sure it was added $this->assertEquals(0, SnippetLanguage::get()->filter('Name', 'Test Language 4')->count(), 'Invalid language was created'); }