<commit_before><?php


namespace CodeExplorerBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use DOMDocument;
use DOMXPath;
use CodeExplorerBundle\Entity\SymfonyControllerElement;

/**
 * Controller used to save source file documentation.
 *
 * @Route("/xmlCodeDoc")
 *
 * @author <NAME>
 */
class CodeExplorerController extends Controller
{
    /**
     * @Route("/save", name="save_code_doc")
     * @Method("POST")
     * @Cache(smaxage="10")
     */
    public function saveAction(Request $request)
    {

        try {
            $content = file_get_contents('php://input');
            $doc = new DOMDocument("1.0", "UTF-8");
            $doc->loadXML($content);
            $docFileElement = $doc->getElementsByTagName('xmlCodeDocFile');
            $docFile = $docFileElement[0]->textContent;
            $codeDocContent=$docFileElement[0]->nextSibling;
            $codeDoc = new DOMDocument("1.0", "UTF-8");
            $newNode = $codeDoc->importNode($codeDocContent,True);
            $codeDoc->appendChild($newNode);
            $res = $codeDoc->save($docFile);
            $response = new Response("<OK>document has been saved.</OK>", 200, array('Content-Type'=> 'application/xml; charset=UTF-8'));
            $response->setCharset("UTF-8");
            if (!$res) {
                $response->setStatusCode(500);
                $response->setContent("<KO>save of code document in file $docFile failed.</KO>");
            }
            return $response;
        } catch (\Exception $e) {
            return new Response("<KO>Exception : code : {$e->getCode()} : {$e->getMessage()}</KO>",500,array('Content-Type'=> 'application/xml; charset=UTF-8'));
        } catch (\Error $e) {
            return new Response("<KO>Exception : code : {$e->getCode()} : {$e->getMessage()}</KO>",500,array('Content-Type'=> 'application/xml; charset=UTF-8'));
        }

    }

    /**
     * @Route("/editor", name="edit_code_doc")
     * @Method("GET")
     * @Cache(smaxage="10")
     */
    public function editAction(Request $request)
    {


            return $this->render('@CodeExplorer/template-based-editor.twig');


    }

    /**
     * @Route("/store", name="store_code_doc")
     * @Method("POST")
     * @Cache(smaxage="10")
     */
    public function storeAction(Request $request)
    {
        //TODO : implement me :
        //??store doc element??, and then :
        try {
            $res=null;
            $content = file_get_contents('php://input');
            $doc = new DOMDocument("1.0", "UTF-8");
            $doc->loadXML($content);
            $srcRelativePath = $doc->getElementsByTagName('srcRelativePath')[0]->textContent;
            $class_name = $doc->getElementsByTagName('class_name')[0]->textContent;
            $method_name = $doc->getElementsByTagName('method_name')[0]->textContent;
            $starting_line = $doc->getElementsByTagName('starting_line')[0]->textContent;
            $ending_line = $doc->getElementsByTagName('ending_line')[0]->textContent;
            $xpath = new DOMXpath($doc);
            $codeDocContent = $xpath->query('content/*')[0];
            $content = $doc->saveXML($codeDocContent);
            $docElement = new SymfonyControllerElement();
            $docElement->setContent($content);
            $docElement->setClassName($class_name);
            $docElement->setMethodName($method_name);
            $docElement->setMethodLineStart($starting_line);
            $docElement->setMethodLineEnd($ending_line);
            $docElement->setGitSourceUrl(SymfonyControllerElement::GIT_SRC_REPO . '/' . $srcRelativePath . '#L' . $starting_line);
            $em = $this->getDoctrine()->getEntityManager('plaidoc');
            $em->persist($docElement);
            $em->flush();
            $response = new Response("<OK>document has been saved.</OK>", 200, array('Content-Type'=> 'application/xml; charset=UTF-8'));
            $response->setCharset("UTF-8");
            return $response;
        } catch (\Exception $e) {
            return new Response("<KO>Exception : code : {$e->getCode()} : {$e->getMessage()}</KO>",500,array('Content-Type'=> 'application/xml; charset=UTF-8'));
        } catch (\Error $e) {
            return new Response("<KO>Exception : code : {$e->getCode()} : {$e->getMessage()}</KO>",500,array('Content-Type'=> 'application/xml; charset=UTF-8'));
        }


    }

}
<commit_msg>CodeExplorerController::actionStore()   - added code to creat vs update the SymfonyControllerElement if an element with the same clasName and methodName exists (take the mos rcent if several).<commit_after><?php


namespace CodeExplorerBundle\Controller;

use CodeExplorerBundle\CodeExplorerBundle;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use DOMDocument;
use DOMXPath;
use CodeExplorerBundle\Entity\SymfonyControllerElement;

/**
 * Controller used to save source file documentation.
 *
 * @Route("/xmlCodeDoc")
 *
 * @author <NAME>
 */
class CodeExplorerController extends Controller
{
    /**
     * @Route("/save", name="save_code_doc")
     * @Method("POST")
     * @Cache(smaxage="10")
     */
    public function saveAction(Request $request)
    {

        try {
            $content = file_get_contents('php://input');
            $doc = new DOMDocument("1.0", "UTF-8");
            $doc->loadXML($content);
            $docFileElement = $doc->getElementsByTagName('xmlCodeDocFile');
            $docFile = $docFileElement[0]->textContent;
            $codeDocContent=$docFileElement[0]->nextSibling;
            $codeDoc = new DOMDocument("1.0", "UTF-8");
            $newNode = $codeDoc->importNode($codeDocContent,True);
            $codeDoc->appendChild($newNode);
            $res = $codeDoc->save($docFile);
            $response = new Response("<OK>document has been saved.</OK>", 200, array('Content-Type'=> 'application/xml; charset=UTF-8'));
            $response->setCharset("UTF-8");
            if (!$res) {
                $response->setStatusCode(500);
                $response->setContent("<KO>save of code document in file $docFile failed.</KO>");
            }
            return $response;
        } catch (\Exception $e) {
            return new Response("<KO>Exception : code : {$e->getCode()} : {$e->getMessage()}</KO>",500,array('Content-Type'=> 'application/xml; charset=UTF-8'));
        } catch (\Error $e) {
            return new Response("<KO>Exception : code : {$e->getCode()} : {$e->getMessage()}</KO>",500,array('Content-Type'=> 'application/xml; charset=UTF-8'));
        }

    }

    /**
     * @Route("/editor", name="edit_code_doc")
     * @Method("GET")
     * @Cache(smaxage="10")
     */
    public function editAction(Request $request)
    {


            return $this->render('@CodeExplorer/template-based-editor.twig');


    }

    /**
     * @Route("/store", name="store_code_doc")
     * @Method("POST")
     * @Cache(smaxage="10")
     */
    public function storeAction(Request $request)
    {
        //TODO : implement me :
        //??store doc element??, and then :
        try {
            $res=null;
            $content = file_get_contents('php://input');
            $doc = new DOMDocument("1.0", "UTF-8");
            $doc->loadXML($content);
            $srcRelativePath = $doc->getElementsByTagName('srcRelativePath')[0]->textContent;
            $class_name = $doc->getElementsByTagName('class_name')[0]->textContent;
            $method_name = $doc->getElementsByTagName('method_name')[0]->textContent;
            $starting_line = $doc->getElementsByTagName('starting_line')[0]->textContent;
            $ending_line = $doc->getElementsByTagName('ending_line')[0]->textContent;
            $xpath = new DOMXpath($doc);
            $codeDocContent = $xpath->query('content/*')[0];
            $content = $doc->saveXML($codeDocContent);
            $em = $this->getDoctrine()->getEntityManager('plaidoc');
            $docRep = $em->getRepository('CodeExplorerBundle:SymfonyControllerElement');
            $docElement=$docRep->findOneBy(array('className' => $class_name, 'methodName' => $method_name),array('id' => 'desc'),1);
            if (!$docElement)
                $docElement = new SymfonyControllerElement();
            $docElement->setContent($content);
            $docElement->setClassName($class_name);
            $docElement->setMethodName($method_name);
            $docElement->setMethodLineStart($starting_line);
            $docElement->setMethodLineEnd($ending_line);
            $docElement->setGitSourceUrl(SymfonyControllerElement::GIT_SRC_REPO . '/' . $srcRelativePath . '#L' . $starting_line);
            $em->persist($docElement);
            $em->flush();
            $response = new Response("<OK>document has been saved.</OK>", 200, array('Content-Type'=> 'application/xml; charset=UTF-8'));
            $response->setCharset("UTF-8");
            return $response;
        } catch (\Exception $e) {
            return new Response("<KO>Exception : code : {$e->getCode()} : {$e->getMessage()}</KO>",500,array('Content-Type'=> 'application/xml; charset=UTF-8'));
        } catch (\Error $e) {
            return new Response("<KO>Exception : code : {$e->getCode()} : {$e->getMessage()}</KO>",500,array('Content-Type'=> 'application/xml; charset=UTF-8'));
        }


    }

}

{'fraction_non_alphanumeric': 0.1657120284416354, 'fraction_numerical': 0.010369346237408652, 'mean_word_length': 2.9713725490196077, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 136, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7658409', 'n_tokens_mistral': 2987, 'n_tokens_neox': 2893, 'n_words': 471}