AmyController::on_save_bundle_snippets PHP Method

on_save_bundle_snippets() public method

public on_save_bundle_snippets ( $pars )
    public function on_save_bundle_snippets($pars)
    {
        $bundle_id = $this->stripFileName($pars['id']);
        $data = $pars['data'];
        if (is_array($data)) {
            $files = array();
            $snippets_dir = $this->configuration['support-path'] . '/bundles/' . $bundle_id . '/snippets/';
            foreach ($data as $snippet_xml) {
                $snippet = new SimpleXMLElement($snippet_xml);
                $filename = trim((string) $snippet->filename);
                $snippet_path = $snippets_dir . $filename . '.amSnippet';
                if ('' == $filename || !file_exists($snippet_path)) {
                    $filename = $this->getFilenameByName((string) $snippet->name);
                }
                $snippet_path = $snippets_dir . $filename . '.amSnippet';
                $yaml_contents = "  name: '" . str_replace("'", "\\'", (string) $snippet->name) . "'\n";
                $yaml_contents .= "  key_activation: '" . str_replace("'", "\\'", (string) $snippet->key_activation) . "'\n";
                $yaml_contents .= "  tab_activation: '" . str_replace("'", "\\'", (string) $snippet->tab_activation) . "'\n";
                $files[$snippet_path] = array($yaml_contents, (string) $snippet->index, $filename);
                $files[$snippet_path . 'Def'] = (string) $snippet->code;
            }
            // cleaning snippets directory
            if (false !== ($d = opendir($snippets_dir))) {
                while (false !== ($f = readdir($d))) {
                    if ('.' != $f && '..' != $f) {
                        unlink($snippets_dir . $f);
                    }
                }
                closedir($d);
            }
            $result = array();
            // saving new files
            foreach ($files as $path => $content) {
                if (is_array($content)) {
                    $index = $content[1];
                    $filename = $content[2];
                    $content = $content[0];
                    $result[] = array('index' => $index, 'filename' => $filename);
                }
                file_put_contents($path, $content);
                chmod($path, 0666);
            }
            //print_r($files);die();
            self::setResult($result);
        }
        self::raiseError('Invalid data posted.');
    }