AmyController::on_save_bundle_commands PHP Метод

on_save_bundle_commands() публичный Метод

public on_save_bundle_commands ( $pars )
    public function on_save_bundle_commands($pars)
    {
        $bundle_id = $this->stripFileName($pars['id']);
        $data = $pars['data'];
        if (is_array($data)) {
            $commands_dir = $this->configuration['support-path'] . '/bundles/' . $bundle_id . '/commands/';
            $files = array();
            foreach ($data as $command_xml) {
                $command = new SimpleXMLElement($command_xml);
                $filename = trim((string) $command->filename);
                $command_path = $commands_dir . $filename . '.amCommand';
                if ('' == $filename || !file_exists($command_path)) {
                    $filename = $this->getFilenameByName((string) $command->name);
                }
                $command_path = $commands_dir . $filename . '.amCommand';
                $yaml_contents = "  name: '" . str_replace("'", "\\'", (string) $command->name) . "'\n";
                $yaml_contents .= "  key_activation: '" . str_replace("'", "\\'", (string) $command->key_activation) . "'\n";
                $yaml_contents .= "  tab_activation: '" . str_replace("'", "\\'", (string) $command->tab_activation) . "'\n";
                $yaml_contents .= "  input_type: '" . str_replace("'", "\\'", (string) $command->input_type) . "'\n";
                $yaml_contents .= "  input_type_or: '" . str_replace("'", "\\'", (string) $command->input_type_or) . "'\n";
                $yaml_contents .= "  output_type: '" . str_replace("'", "\\'", (string) $command->output_type) . "'\n";
                $yaml_contents .= "  script_env: '" . str_replace("'", "\\'", (string) $command->script_env) . "'\n";
                $files[$command_path] = array($yaml_contents, (string) $command->index, $filename);
                $files[$command_path . 'Def'] = (string) $command->code;
            }
            // cleaning commands directory
            if (false !== ($d = opendir($commands_dir))) {
                while (false !== ($f = readdir($d))) {
                    if ('.' != $f && '..' != $f) {
                        unlink($commands_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.');
    }